Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: keys and values order on a hash

by ikegami (Pope)
on Jul 11, 2011 at 18:29 UTC ( #913744=note: print w/ replies, xml ) Need Help??


in reply to Re^2: keys and values order on a hash
in thread keys and values order on a hash

You'll get that situation if you have something like

while (...each(%h)...) { ... last if ...; ... }

The solution is

keys(%h); # Reset iterator. while (...each(%h)...) { ... last if ...; ... }

Note that keys does not actually compute the list of keys in void context.


Comment on Re^3: keys and values order on a hash
Select or Download Code
Re^4: keys and values order on a hash
by Somni (Friar) on Jul 11, 2011 at 18:50 UTC
    Indeed, you can insert a keys call prior to using 'each' in every instance, presuming you're not already in the middle of an 'each' on that hash already, and thereby reset the iterator mid-stream:

    my %h = qw( one 1 two 2 three 3 four 4 ); while (my($k, $v) = each %h) { say "$k=$v"; frobnicate(\%h); } sub frobnicate { keys %{ $_[0] } }

    Admittedly, this example is a bit contrived, but it's generally unexpected that your loop becomes infinite just from simple calls that don't appear to modify the hash. The workaround is obvious, if restricting: don't pass the hashref to any functions (including functions with prototypes that auto-enreference it). It's even worse if the hash is global; you essentially can't call any functions, for fear that, somewhere in the call chain, something will call 'keys' on the hash.

    In my opinion, it's too many caveats to make 'each' very useful.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-08-22 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (144 votes), past polls