in reply to sorting an array of hashes by values but ...

You want the Schwartzian Transform. It looks to me like you want to sort the hash references by the sums of their values. (If I'm incorrect, you still probably want the Transform. You just won't be able to use my code. :)
my @hashes = ( { key1 => 1, key2 => 4 }, { key3 => 8, key4 => 4, key5 => 6 }, { key6 => 3 }, ); sub sum { my $sum; $sum += $_ for (@_); return $sum; } @hashes = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, sum( values %$_) ] } @hashes; use Data::Dumper; print Dumper(\@hashes);
It takes a bit of thought to figure it out, but once you do, you'll have scary Perl kung-fu. The trick is adding extra data, sorting on that extra data, then discarding the extra data.

Replies are listed 'Best First'.
Re: Re: sorting an array of hashes by values but ...
by slayven (Pilgrim) on Feb 08, 2001 at 23:19 UTC
    wow
    i never understood (well, actually i've never tried to understand) the schwartzian transform. it has always been far above my skills
    tnx chromatic for forcing me to figure it out by solving one of my problems.

    now i can feel the source in me ;)

    slay