This will work as long as all the values in your hash are unique. Reverse
the hash into a new hash. You now have a hash where the original values are
the keys, and the original keys are the value.
%hash = ( 'example' => 1,
'for' => 3,
'this' => 6,
'piece' => 2,
'of' => 5,
'code' => 4,
);
%reversed = reverse %hash;
foreach $key(sort keys %reversed) {
print "Key is $reversed{$key}. Value is $key\n";
}
-----Original Message-----
From: Mark Bedish [mailto:[EMAIL PROTECTED]]
Sent: 26 June 2001 12:21
To: [EMAIL PROTECTED]
Subject: sort by value?
I have a very simple tab delimited file containing text and numbers ,
just the 2 columns and I would like to sort by ascending numeric. I have
checked Learning Perl and Prog Perl but cannot get it to work - is that
because you can only sort by key or am I using the wrong approach
entirely?
my @fields = qw/Text Time/;
my %ch;
foreach my $key (sort { $ch{1} cmp $ch{2} } keys %ch ) {
print OUT "$key: $ch{$key} \n"; # show key and value
}
Thanks,
Mark Bedish
--------------------------Confidentiality--------------------------.
This E-mail is confidential. It should not be read, copied, disclosed or
used by any person other than the intended recipient. Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful. If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.