Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^2: The Anomalous each()

by fletcher_the_dog (Friar)
on Nov 18, 2005 at 20:13 UTC ( #509934=note: print w/replies, xml ) Need Help??

in reply to Re: The Anomalous each()
in thread The Anomalous each()

But you do have to worry if you for some reason call 'each' or 'keys' on a hash in an inner loop of an each loop. There is not really any way around this expect to use 'keys' in your outer loop instead of 'each'.
# this code creates an endless loop my %hash = ( foo=>"x", Foo=>"y", Bar=>"a", bar=>"c" ); while(my($key,$value)=each %hash){ # dumb example for my $key2 (keys %hash){ if(lc($key) eq lc($key2)){ print "key $key is similiar to $key2\n"; } } }

Replies are listed 'Best First'.
Re^3: The Anomalous each()
by diotalevi (Canon) on Nov 18, 2005 at 20:15 UTC
    Ok, so don't give your hash to functions you don't control if you think that's going to be a problem.
      Better wording would be:
      Caution: Don't use each() for a loop if you have to hand your hash to a function unless you know for sure that the function definitely won't use each()/keys()/values() on it. (It's probably a sure bet that if you hand off a hash to a function it will do just that.)

        I tend not to put actual labels that say "Caution: ..." but its reasonable that there be some appropriate language in the documentation.

        Anyhow, its fair enough. *wink*

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2022-08-08 03:56 GMT
Find Nodes?
    Voting Booth?

    No recent polls found