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*