Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Neither foreach nor while implicitly empty arrays. I recommend reading through both perlintro, and then perlsyn to understand Perl's looping constructs better. Particularly, perlsyn give a detailed discussion of Perl's looping mechanisms.

If the behavior you're after is to empty an array, you can use shift or pop to remove one element at a time in a loop, like this:

while ( @array ) { my $element = shift @array; # or "my $element = pop @array;". # Now, do something with $element }

Loops just loop, that's all they do. In the case of for or foreach, they iterate over a list (or an array). In the case of while, they loop until a test condition is false. In the case of until, they loop until a test condition is true.

In the case of foreach ( @array ) loops, while the loop itself is non-destructive, the special variable $_ is aliased to each element in the array, one by one as foreach iterates over the array (or list). This means that if you modify $_, the effect will ripple back into the array over which you're iterating, so be careful. This also applies to the iterator variable in foreach loops, even if a named iterator is declared, eg. "foreach my $element ( @array ) {....". Also note that it is almost always a bad idea to add or remove elements from an array while looping over it via foreach as it leads to ambiguity such as "are you looping over the newly resized array, or the original?"


Dave


In reply to Re: how do foreach and while affect an array? by davido
in thread how do foreach and while affect an array? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-23 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found