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

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?
  • Comment on Re^2: Building an array of hashes, and then sorting keys with each hash

Replies are listed 'Best First'.
Re^3: Building an array of hashes, and then sorting keys with each hash
by ikegami (Patriarch) on Dec 30, 2009 at 12:41 UTC
    Run it...
      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.

      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 want to try to retrieve the best match/(row) for each ID based on their top score.

        You want to group the records by id, and sort each group by score.

        Hashes are great for grouping.

        my %rows_by_id; while (<CBLAT>) { chomp; my %row; @row{qw( score id )} = ( split /\t/ )[ 0, 9 ]; my $id = $row{id}; push @{ $rows_by_id{$id} }, \%row; } for my $id (keys %rows_by_id) { # Add "sort" if desired my $rows = $rows_by_id{$id}; @$rows = sort { $b->{score} <=> $a->{score} } @$rows; my $best_row = $rows->[0]; print("Best score for id $id: $best_row->{score}\n"); }