Endless Loop (trying for...)

B

Brian

Shouldn't -

my @a;
my $y = 1;
push (@a, $y);
foreach my $x (0..$#a) {
print "array = @a\n";
$y++;
push (@a, $y);
}

result in an endless loop? And if not (I can't get it to) how do I
create an endless loop like that above?

I want to parse through an array, but change the size of the array at
the same time. Don't worry, actual program will have checks and
balances so it does not run till the end of time.
 
M

Mike Hunter

Shouldn't -

my @a;
my $y = 1;
push (@a, $y);
foreach my $x (0..$#a) {
print "array = @a\n";
$y++;
push (@a, $y);
}

result in an endless loop? And if not (I can't get it to) how do I
create an endless loop like that above?

I want to parse through an array, but change the size of the array at
the same time. Don't worry, actual program will have checks and
balances so it does not run till the end of time.

My uninformed guess is that (0..$#a) is evaluated once upon startup.
 
B

Brian Harnish

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Shouldn't -

my @a;
my $y = 1;
push (@a, $y);
foreach my $x (0..$#a) {
print "array = @a\n";
$y++;
push (@a, $y);
}

result in an endless loop? And if not (I can't get it to) how do I
create an endless loop like that above?

No, 0..$#a is only evaluated at the beginning of the loop. You would want
to use the c-style for syntax, where the condition is evaluated each time.

for(my $x = 0; $x <= $#a; $x++) {
push(@a,$y);
}

- Brian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/eLCXiK/rA3tCpFYRAvn7AKDaIAsuOvEXHGWgNPn3yAt69LtI3ACgnujD
FR0hQ5DleXV0ZpqUOM7Oaj4=
=vVby
-----END PGP SIGNATURE-----
 
D

Darren Dunham

Brian said:
Shouldn't -
my @a;
my $y = 1;
push (@a, $y);
foreach my $x (0..$#a) {
print "array = @a\n";
$y++;
push (@a, $y);
}
result in an endless loop? And if not (I can't get it to) how do I
create an endless loop like that above?

Well, by using $#a, you're giving a specific set of indexes (0 and
whatever $#a is) to operate on.

If you want to operate on an array, operate on an array. This is how I
would have done it in the first place. Don't mess with indexes if you
don't want to mess with indexes...

my $y = 1;
my @a = ($y);
foreach my $x (@a)
{
print "array = @a\n";
print "index variable= $x\n";
$y++;
push (@a, $y);
}
 
K

ko

Brian said:
Shouldn't -

my @a;
my $y = 1;
push (@a, $y);
foreach my $x (0..$#a) {
print "array = @a\n";
$y++;
push (@a, $y);
}

result in an endless loop? And if not (I can't get it to) how do I
create an endless loop like that above?

I want to parse through an array, but change the size of the array at
the same time. Don't worry, actual program will have checks and
balances so it does not run till the end of time.

You can use a while loop:

while (@a) {
print "@a\n";
parse_sub($a[$y]); # your array parsing sub
push @a, $y++;
last if $y > 5; # your end condition
}
 
B

Bob Walton

Darren said:
....


my $y = 1;
my @a = ($y);
foreach my $x (@a)
{
print "array = @a\n";
print "index variable= $x\n";
$y++;
push (@a, $y);
}

Hmmmm...from the perlsyn docs for foreach:


"If any part of LIST is an array, foreach will get very confused if you
add or remove elements within the loop body, for example with splice. So
don't do that."

So maybe that isn't a good idea, even if it does seem to work in some
particular version and platform of Perl.
 
B

Brian

Wow - What a massive lesson in arrays and loops. You perl-nuts ROCK!
Thanks. Most of those examples worked great. As for the one's I
couldn't get to work (I was just messing around once it was
functional) were more than likely due to the fact that they were above
my head.

Again thanks!

Purl Gurl: "I don't even see the perl code anymore - I just see
blonde, brunette, redhead..."
 
B

Bill

Purl Gurl said:
A couple more variations which violate this typical
Perl 5 Cargo Cult dogma. First example uses unshift
and pop, in violation of this rule. Second example
uses push and shift, also in violation of this rule.

Actually, the problem is that the underlying implementation can have
its position pointers confused if splicing which changes array size is
done inside the array. But push and pop only change the head or tail
of the list, and therefore you get away with them in the foreach,
since the foreach check will look for end-of-list or end-of-array.

Try a big list, where you cut and paste large numbers of elements in
the midst of the list, as an example instead?
 
J

John W. Krahn

Brian said:
Shouldn't -

my @a;
my $y = 1;
push (@a, $y);
foreach my $x (0..$#a) {
print "array = @a\n";
$y++;
push (@a, $y);
}

result in an endless loop? And if not (I can't get it to) how do I
create an endless loop like that above?

An endless loop is usually written as:

while ( 1 ) { ... }

Or:

for ( ;; ) { ... }

Or:

{ ...; redo }



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top