in reply to Re^4: 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
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"); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Building an array of hashes, and then sorting keys with each hash
by odegbon (Initiate) on Jan 03, 2010 at 22:19 UTC | |
by ikegami (Patriarch) on Jan 03, 2010 at 22:46 UTC | |
by odegbon (Initiate) on Jan 04, 2010 at 00:07 UTC | |
by ikegami (Patriarch) on Jan 04, 2010 at 03:32 UTC |