On Thu, May 31, 2001 at 02:41:32PM -0500, David Michael wrote:
> Hello,
> I have a hash that needs to be displayed in a certain order. I tried
>
> foreach $key (sort (keys %HASH)) {
> print $key;
> }
>
> that sorts alphabetically. I need it in the order it was inserted, so I made the
>value a number that increased for each key. I need to sort by the value, but display
>the key. Any ideas ?
You want something like
foreach $value(sort {$a <=> $b} (values(%hash)){
print $value;
}
The {$a <=> $b} thing is an anonymous subroutine that specifies a
numeric sort. You could also do
foreach $value(sort byNum (values(%hash)){
print $value;
}
sub byNum {
$a <=> $b;
}
"perldoc -f sort" ought to help if you need more info.
cheers
rob c