Thanks in advance for the help.
I have a array called %pepHash with 174 ‘counts’ and multiple ‘peptides’ in each $count.
Here’s a snippet of the hash:
Count: 39 Peptide: 0 Score: 55.03 MR: 1792.3206
Count: 39 Peptide: 1 Score: 75.22 MR: 1792.6158
Count: 39 Peptide: 2 Score: 62.63 MR: 1972.7156
Count: 39 Peptide: 3 Score: 49.95 MR: 2365.2174
Count: 40 Peptide: 0 Score: 46.38 MR: 1256.4437
Count: 40 Peptide: 1 Score: 71.07 MR: 1950.1644
Count: 40 Peptide: 2 Score: 71.77 MR: 2492.9394
Count: 40 Peptide: 3 Score: 67.28 MR: 2493.0154
Firstly, I’m not very good with data structures, so any better advice on multi-d hashes would be nice.
Count: 40 Peptide: 1 Score: 71.07 MR: 1950.1644
is accessed by $pepHash[$count][$pepCount]{$PEP_SCORE} or {$PEP_MR}, and in this case it’s obviously $pepHash[40][1]{$PEP_SCORE}.
I want to sort all of %pepHash by its scores in descending order or create a new sorted hash so the list becomes:
Count: 39 Peptide: 0 Score: 75.22 MR: 1792.6158
Count: 39 Peptide: 1 Score: 62.63 MR: 1972.7156
Count: 39 Peptide: 2 Score: 55.03 MR: 1792.3206
Count: 39 Peptide: 3 Score: 49.95 MR: 2365.2174
Count: 40 Peptide: 0 Score: 71.77 MR: 2492.9394
Count: 40 Peptide: 1 Score: 71.07 MR: 1950.1644
Count: 40 Peptide: 2 Score: 67.28 MR: 2493.0154
Count: 40 Peptide: 3 Score: 46.38 MR: 1256.4437
Notice the updated $pepCount values. I have this loop which could be of use:
for ($count = 0; $count < $total; $count++) {
for ($pepCount = 0; $pepCount < $pepTotal[$count]; $pepCount++) {
}
}
I was also messing with this to no avail cause I’m a noob:
foreach $key (sort {$pepHash{$b} <=> $pepHash{$a}} keys(%pepHash)) {
#CONFUSED
}
EDIT: This helped me a lot: http://www.stathis.co.uk/computers/perl-sort-tutorial
To store such data structures you may use hash of hashes. Here’s example:
And then sort this hash:
You may also want to read Perl documentation to feel yourself better with Perl data structures and perldoc -f sort to learn more about sorting in Perl.
Good luck!