Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

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

It's not a difference between a foreach loop and each; it's not a difference between a foreach loop and a while loop.

A foreach loop is used when we need to iterate a number of times which is known up front.

On the other hand, a while loop allows us to loop a number of times that isn't known ahead of time. An expression is repeatedly evaluated to determine if the loop should be entered again. It MUST be evaluated each time. It wouldn't work otherwise.

So why does following work:

while ( my ( $k, $v ) = each( %h ) ) { ... }

Each array and hash has an iterator associated with it which is used by each, keys and values. Each call to each fetches one element from that iterator (resetting the iterator once the end is reached).

So then what about the following:

while ( my ( $k, $v ) = each( %{ create_new_hash() } ) ) { ... }

A different, newly-created hash is passed to each each time. Each of those hashes has a fresh iterator. Each of those iterator returns the first element of that hash.

Giving each array and hash its own iterator allows us to use

while ( my ( $k, $v ) = each( %h ) ) { ... }

instead of having to use

my $iter = get_iter( \%hash ); while ( my ( $k, $v ) = $iter->() ) { ... }

What some other newer languages have done instead is provide syntactical support for iterators. The equivalent of the previous snippet is what's executed.

using ( var iter = dict.GetEnumerator() ) { while ( iter.MoveNext() ) { var entry = iter.Current; ... } }

But a shorthand is provided by the language.

foreach ( var entry in dict ) { ... }

You can build iterators in Perl, but it lacks the syntactical support shown here.

In reply to Re: Why does each() always re-evaluate its argument? by ikegami
in thread Why does each() always re-evaluate its argument? by Darkwing

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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-24 06:33 GMT
Find Nodes?
    Voting Booth?

    No recent polls found