http://qs321.pair.com?node_id=243318


in reply to Re: Re: Printing a hash in a specific order?
in thread Printing a hash in a specific order?

Here's a more efficient idiom for grabbing a single key from a hash reference:

my $key = each %{$hashref};

The approach you're using with my($key) = keys %{$hashref}; essentially generates a list of the keys in memory, assigns the first element in the list to $key, and then throws rest of the list out. If the size of the hash is large this could be wasteful, but even with small hashes there is a noticeable difference between the two idioms when benchmarking.

Dan Kubb, Perl Programmer

Updated: Made minor corrections noted by Limbic-Region