in reply to Building an array of hashes, and then sorting keys with each hash

my program seem to be processing only the first row.

The rows are in @CHIMP. You grab the first row using my $cblast = shift @CHIMP;. You only execute that once, and you never touch @CHIMP again.

$score is never assigned a value, yet you use it all over the place. Boooo! for disabling warnings or ignoring them. I think you meant $hITS{score} rather than $hITS{$score}.

Anyway, this is what you want:

my @hITS; while (<CBLAT>) { chomp; my ($score, $qID) = ( split /\t/ )[ 0, 9 ]; push @hITS, [ $_, $score, $qID ]; } @hITS = sort { $b->[1] <=> $a->[1] || $a->[2] cmp $b->[2] } @hITS; for (@hITS) { print $_->[0], "\n"; }

Update: I suppose you did say AoH. It's more readable, especially if you save the other fields:

my @hITS; while (<CBLAT>) { chomp; my %row; @row{qw( score qID )} = ( split /\t/ )[ 0, 9 ]; push @hITS, \%row; } @hITS = sort { $b->{score} <=> $a->{score} || $a->{qID} cmp $b->{qID} } @hITS; for (@hITS) { print "score: $_->{score} qID: $_->{qID}\n"); }

Replies are listed 'Best First'.
Re^2: Building an array of hashes, and then sorting keys with each hash
by odegbon (Initiate) on Dec 30, 2009 at 07:05 UTC

    thanks.

    I am also interested in printing/retrieving the entire row after sorting as in the last line above. Does $_ give the entire row, sorted?
      Run it...
        I have a scenario where everything in my hits(rows) is the same as above except that I now have 2 different IDs in the ID column . I want to try to retrieve the best match/(row) for each ID based on their top score. For instance, the actual IDs in hits - pwc and pwz respectively.

        I have tried to sort IDs first and then scores:

        <code>@hITS = sort ($a->{qID} cmp $b->{qID} || $b->{score} <=> $a->{score})@hITS;

        now I would like to retrieve best score/row for each ID:

        I have 2 different IDs in my hits and I am trying to retrieve best hit for each ID based on best scores as in my post before this.

        I have tried to do something like this:

        $hITS[0]{score} = $score; $hITS[0]{qID} = $Id; for (@hITS){</p if ($_->{qID} !~ /$id/ && $_->{score} <= $score){</p> $_->{qID} =$id1 ; $_->{score} = $score1;

        not sure about the above.