in reply to Best method to order a hash of arrays

Well, all very interesting, thanks, but I think I'm going to have to go with
<code> # code => [ 'label', 'order' ] %user_types =( 037 => [ 'member', '1' ], 165 => [ 'public', '2' ], 022 => [ 'staff', '3' ], 683 => [ 'babe', '4' ], 001 => [ 'old fart', '5' ] );
and
@types_sorted = sort { $user_types{$a}[1] <=> $user_types{$b}[1] } ke +ys %user_types; for $i (0 .. $#types_sorted) { make options list; }
It's simple & readable




Forget that fear of gravity,
Get a little savagery in your life.

Replies are listed 'Best First'.
Re^2: Best method to order a hash of arrays
by jdporter (Paladin) on May 15, 2007 at 21:55 UTC

    You should be aware that your code, above, creates a hash with keys "37", "165", "22", "683", "1".
    Unfortunately, if you want the leading zeroes, you'll need to explicitly quote the keys.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight

      Actually, it's worse than that. The leading zero causes Perl to interpret it as an octal value, so 037 becomes '31'.

      use Data::Dumper; my %user_types =( 037 => [ 'member', '1' ], 165 => [ 'public', '2' ], 022 => [ 'staff', '3' ], 683 => [ 'babe', '4' ], 001 => [ 'old fart', '5' ] ); print Dumper( \%user_types ); __END__ $VAR1 = { '1' => [ 'old fart', '5' ], '18' => [ 'staff', '3' ], '165' => [ 'public', '2' ], '683' => [ 'babe', '4' ], '31' => [ 'member', '1' ] };