in reply to Sorting or Aligning Parallel Hashes
Your analysis of the problem is quite accurate. If you do those steps, one at a time, you get what you want. Keep in mind that hashes can't be sorted, just their keys and/or values.
I would like to sort Hash “A” (key) in descending order
my @sorted_scores = sort { $b <=> $a } keys %HashA;
and then align the (value) Hash “A” with Hash “B” (key).
my @sorted_words = map { $HashA{$_} } @sorted_scores;
Then use that sort, to align Hash “C” so that the key value pairs of "C" can be printed out in the same order as the sorted “A” Hash.
my @sorted_line_nums = map { $HashB{$_} } @sorted_words;
And the print:
print($HashC{$_}, "\n"); foreach @sorted_line_nums;
All together now:
print("$_\n"); foreach map { $HashC{$HashB{$HashA{$_}}} } sort { $b <=> $a } keys %HashA;
I'm curious as to how you handle two lines having the same score, if the score is the key.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting or Aligning Parallel Hashes
by Gavin (Archbishop) on Mar 29, 2006 at 11:12 UTC | |
by ikegami (Patriarch) on Mar 29, 2006 at 17:10 UTC | |
by bart (Canon) on Apr 03, 2006 at 09:05 UTC | |
by ikegami (Patriarch) on Apr 03, 2006 at 14:09 UTC |