Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: mystery re each + last

by merlyn (Sage)
on Sep 28, 2005 at 15:08 UTC ( [id://495784]=note: print w/replies, xml ) Need Help??


in reply to mystery re each + last

From perldoc -f each:
When the hash is entirely read, a null array is returne +d in list context (which when assigned produces a false (0) +value), and "undef" in scalar context. The next call to "each" + after that will start iterating again. There is a single ite +rator for each hash, shared by all "each", "keys", and "value +s" func- tion calls in the program; it can be reset by reading a +ll the elements from the hash, or by evaluating "keys HASH" or + "values HASH".
So, your problem is that you're leaving the "single iterator" in the middle of the hash, and not resetting it. You'll need to reset it before entering the next loop.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: mystery re each + last
by inq123 (Sexton) on Sep 28, 2005 at 15:49 UTC
    BTW, forgot to mention a derivative from the answer, and a complaint about this 'each' property:

    1. Since there's an iterator for each hash, if I had only used 'last' in the inner while loop, then only the inner while loop cannot be re-entered.

    2. Actually my script did not leave the iterator in the middle of the hash (it already read the only element in the hash before calling 'last'), it's just that 'each' can only be reset after calling 'each' ONE MORE time AFTER all elements were read. So the documentation above should be modified (because it incorrectly stated "it can be reset by reading all the elements from the hash", but my script did read all elements in the hash!).

    Well, just want to be picky, after wasting much time on this error-prone iterator for 'each'. :)

Re^2: mystery re each + last
by radiantmatrix (Parson) on Sep 28, 2005 at 15:20 UTC

    A truly elegant and impeccably correct answer! I wish I could ++ you more than once for that one. The above is a fantastic example of how to answer factual questions: it provides the correct information, explains it briefly and in plain language, and explains how the information was found.

    Bravo, sir! Bravo!

    <-radiant.matrix->
    Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law
Re^2: mystery re each + last
by inq123 (Sexton) on Sep 28, 2005 at 15:21 UTC
    Interesting. Thanks! I guess the lesson is never use each and last together ('cause one'd probably forget about resetting).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 17:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found