--- Mark Bedish <[EMAIL PROTECTED]> wrote:
> 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
>  }

The problem is in this line:

  sort { $ch{1} cmp $ch{2} }  keys %ch

sort() receives the list of keys from keys %ch, and passes pairs to be
compared to the code block { $ch{1} cmp $ch{2} } as $a and $b. Try
this:

  sort { $ch{$a} cmp $ch{$b} }  keys %ch

Which will use the $a and $b passed as keys to look up values in the
hash, and will return the list of keys sorted by the associated values.

As always, somebody correct anything I goof, because I'm doing this off
the cuff. =o)


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

Reply via email to