http://qs1969.pair.com?node_id=686533


in reply to Deterministic or keys in random order

Just to be explict. You do realise that the fact Zero is returned as the first key has nothing to do with the order of insertion?

All of these print "Zero" on my build of perl (5.8.7)

my %hash = ( "Zero" =>"0", "One" => "1", "Two" => "2", ); my @array = keys %hash; print $array[0] . "\n"; -------------------------- my %hash = ( "One" => "1", "Zero" =>"0", "Two" => "2", ); my @array = keys %hash; print $array[0] . "\n"; -------------------------- my %hash = ( "Two" => "2", "One" => "1", "Zero" =>"0", ); my @array = keys %hash; print $array[0] . "\n";
As a Perl programmer you should never write code which relies on the order returned by keys/each (apart from it being the same as the list time you checked, if you’ve not updated the array).
---
my name's not Keith, and I'm not reasonable.

Replies are listed 'Best First'.
Re^2: Deterministic or keys in random order
by andreas1234567 (Vicar) on May 14, 2008 at 18:29 UTC
    As a Perl programmer you should never write code which relies on the order returned by keys/each.
    That's exactly why I think this particular exam question is flawed.
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]