in reply to Comparing all keys in a hash
A nit - don't sort the arrays until you've done your @k1 <=> @k2 test. If that returns true then you wasted two sort() ops. Also, do your cmp operation once and stash the result in a my() variable. Its fast, easy and saves redundant work.
sub compare_hash_keys { my ($h1,$h2) = @_; my $test; # A temporary variable for holding a result so it doesn' +t have to be recomputed. my @k1 = keys %$h1; my @k2 = keys %$h2; $test = @k1 <=> @k2 and return $test; for ( 0 .. $#k1 ) { $test = $k1[$_] cmp $k2[$_] and return $test; } return; }
|
|---|