Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Endless Loop (trying for...)

Reply
Thread Tools

Endless Loop (trying for...)

 
 
Brian
Guest
Posts: n/a
 
      09-29-2003
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.
 
Reply With Quote
 
 
 
 
Mike Hunter
Guest
Posts: n/a
 
      09-29-2003
On 29 Sep 2003 15:07:30 -0700, Brian wrote:
> 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.
 
Reply With Quote
 
 
 
 
Brian Harnish
Guest
Posts: n/a
 
      09-29-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, 29 Sep 2003 15:07:30 -0700, Brian wrote:

> 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-----

 
Reply With Quote
 
Darren Dunham
Guest
Posts: n/a
 
      09-29-2003
Brian <> wrote:
> 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);
}

--
Darren Dunham
Unix System Administrator Taos - The SysAdmin Company
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
 
Reply With Quote
 
ko
Guest
Posts: n/a
 
      09-30-2003
Brian wrote:

> 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
}

 
Reply With Quote
 
Bob Walton
Guest
Posts: n/a
 
      09-30-2003
Darren Dunham wrote:

> Brian <> wrote:
>

....


> 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.

--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

 
Reply With Quote
 
Brian
Guest
Posts: n/a
 
      09-30-2003
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..."
 
Reply With Quote
 
Bill
Guest
Posts: n/a
 
      09-30-2003
Purl Gurl <> wrote in message news:<>...
> Purl Gurl wrote:
>
> > Bob Walton wrote:
> > > Darren Dunham wrote:
> > > > Brian wrote:

>
> (snipped)
>
> > > "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."

>
> 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?
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      09-30-2003
Brian wrote:
>
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Can a simple a==b 'hang' in and endless loop? Claudio Grondi Python 75 01-21-2006 04:19 AM
endless loop and i dont understand why Alexandre Java 4 04-12-2005 07:20 PM
Endless loop on delete auth cookie/abandon session =?Utf-8?B?QmlsbCBCb3Jn?= ASP .Net 0 02-02-2005 08:05 PM
exit endless python loop in emacs Leo Python 1 07-04-2003 12:58 PM



Advertisments