Endless Loop (trying for...)

Discussion in 'Perl Misc' started by Brian, Sep 29, 2003.

  1. Brian

    Brian Guest

    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.
     
    Brian, Sep 29, 2003
    #1
    1. Advertising

  2. Brian

    Mike Hunter Guest

    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.
     
    Mike Hunter, Sep 29, 2003
    #2
    1. Advertising

  3. -----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-----
     
    Brian Harnish, Sep 29, 2003
    #3
  4. 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. >
     
    Darren Dunham, Sep 30, 2003
    #4
  5. Brian

    ko Guest

    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
    }
     
    ko, Sep 30, 2003
    #5
  6. Brian

    Bob Walton Guest

    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
     
    Bob Walton, Sep 30, 2003
    #6
  7. Brian

    Brian Guest

    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..."
     
    Brian, Sep 30, 2003
    #7
  8. Brian

    Bill Guest

    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?
     
    Bill, Sep 30, 2003
    #8
  9. 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
     
    John W. Krahn, Sep 30, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmlsbCBCb3Jn?=

    Endless loop on delete auth cookie/abandon session

    =?Utf-8?B?QmlsbCBCb3Jn?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    687
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 2, 2005
  2. Alexandre
    Replies:
    4
    Views:
    562
    Lasse Reichstein Nielsen
    Apr 12, 2005
  3. Leo
    Replies:
    1
    Views:
    437
    Thomas Heller
    Jul 4, 2003
  4. Claudio Grondi
    Replies:
    75
    Views:
    1,146
    Tim Peters
    Jan 21, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    390
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page