On Wed, 6 Feb 2002, birgit kellner wrote:

> my %hash = (
>       'keyone' => ['firstvalueone', 'firstvaluetwo],
>       'secondkey' => ['anothervalueone', 'valuetwoforsecondkey'],
>       'keythree' => ['thirdvalueone', 'thirdvaluetwo']
>
> );
>
> Can I sort the hash on the second element of the anonymous array?

Sure, use the sort function:

#!/usr/local/bin/perl -w

use strict;

my %hash = (
        'keyone' => ['firstvalueone', 'firstvaluetwo'],
        'secondkey' => ['anothervalueone', 'valuetwoforsecondkey'],
        'keythree' => ['thirdvalueone', 'thirdvaluetwo']

);


my @result = sort {$hash{$a}->[1] cmp $hash{$b}->[1]} keys %hash;

foreach(@result) {

        print "$hash{$_}->[1]\n";
}

Which prints out:

~$ ./sortme.pl
firstvaluetwo
thirdvaluetwo
valuetwoforsecondkey

-- Brett
                                          http://www.chapelperilous.net/
------------------------------------------------------------------------
bug, n:
        A son of a glitch.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to