ThingFish has asked for the wisdom of the Perl Monks concerning the following question:

I've stored multiple scalars into an array reference in each hash key, like so:

 push( @{$user_hash{$user}}, ($age, $height, $weight) );

Now I would like to print each hash key and values sorted by any given scalar. (Such as listing each user in ascending order by age)

But I cant figure how.

Any suggestions?

  • Comment on Sort multivalued hash by value in array reference

Replies are listed 'Best First'.
Re: Sort multivalued hash by value in array reference
by knobunc (Pilgrim) on Feb 27, 2002 at 18:25 UTC
    use strict; ... foreach my $user (sort { $user_hash{$a}[0] <=> $user_hash{$b}[0] } keys %user_hash) { my ($age, $height, $weight) = @{ $user_hash{$user} }; print "User $user (age $age, height $height, weight $weight)\n"; }

    -ben