Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: infinite loop on while (@array)

by cchampion (Curate)
on Mar 27, 2002 at 07:40 UTC ( [id://154613]=note: print w/replies, xml ) Need Help??


in reply to infinite loop on while (@array)

If you want to change a variable, then use it in addition to the array.
my $count = 10; while ($count) { #do something to array print $array[$count--]; } # alternative $count =0; for (@array) { print "$count $_\n"; # $_ is the current array item $count = &something_complex; }
Anyway, the loop you want to use is an infinite loop, unless you empty the array. It will test TRUE as long as the array has more than 0 elements.
If you need a real infinite loop, then this one could suit you better:
my $count =0; while(1) { # do something to the array $count = &something_strange last if (test_exit_condition); }

Replies are listed 'Best First'.
Re: Re: infinite loop on while (@array)
by Juerd (Abbot) on Mar 27, 2002 at 07:44 UTC

    If you need a real infinite loop, then this one could suit you better:

    Or the expressionless C-style loop. (The while(1) loop will be converted into it to optimize, so why not type it yourself?) They're a nice visual way to say: this is infinite, while while(1) and while(0) don't differ a lot.

    for (;;) { # ... }

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

      <pedant class="minor">

        The while(1) loop will be converted into it to optimize

        Im fairly sure that if you look into the documentation again that the c style for loop is actually converted into a while() block. Not the other way round.

        Hmm, just doublechecked perlsyn and it seems that they arent clear, nor is the camel, as to whether these are actually handled internally as forms of each other. However the camel has the following interesting addition to the perlsyn:

        The for loop can be defined in terms of the corresponding while loop.

        Thus, the following:

        for ($i = 1; $i < 10; $i++) { ... }
        is the same as:

        $i = 1; while ($i < 10) { ... } continue { $i++; }
        (Defining the for loop in terms of a continue block allows us to preserve the correct semantics even when the loop is continued via a next statement. This is unlike C, in which there is no way to write the exact equivalent of a continued for loop without chicanery.)
        <super>emphasis added by me</super>
      </pedant>

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

        Using B::Deparse under ActivePerl b629 (aka 5.6.1)...

        C:\> perl -MO=Deparse -e "while (1) { print 1; }" for (;;) { print 1; } -e syntax OK C:\> perl -MO=Deparse -e "for (;;) { print 1; }" for (;;) { print 1; } -e syntax OK

        But if I try the for example that demerphq quotes under Perl 5.5.3 (on FreeBSD), I get ...

        $ perl -MO=Deparse -le 'for ( my $i = 0; $i < 10; $i++ ) { print 1 }' -e syntax OK my $i = 0; while ($i < 10) { print 1 } continue { ++$i }

        Hmmm...

            --k.


Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://154613]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-29 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found