在 Perl 中按特定键中的值对多维哈希进行排序
预先感谢您的帮助。
我有一个名为 %pepHash
的数组,每个 $count
中有 174 个“计数”和多个“肽”。 这是哈希的片段:
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
首先,我不太擅长数据结构,因此任何关于多维哈希的更好建议都会很好。
Count: 40 Peptide: 1 Score: 71.07 MR: 1950.1644
通过 $pepHash[$count][$pepCount]{$PEP_SCORE}
或 {$PEP_MR}
访问,在本例中显然是 $pepHash[40 ][1]{$PEP_SCORE}
。 我想按分数降序对所有 %pepHash
进行排序,或者创建一个新的排序哈希,使列表变为:
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
注意更新的 $pepCount
值。我有这个可能有用的循环:
for ($count = 0; $count < $total; $count++) {
for ($pepCount = 0; $pepCount < $pepTotal[$count]; $pepCount++) {
}
}
我也搞乱了这个但无济于事,因为我是个菜鸟:
foreach $key (sort {$pepHash{$b} <=> $pepHash{$a}} keys(%pepHash)) {
#CONFUSED
}
编辑:这对我很有帮助:http://www.stathis.co.uk/computers/perl-sort-tutorial
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要存储此类数据结构,您可以使用散列的散列。下面是示例:
然后对这个哈希进行排序:
您可能还需要阅读 Perl 文档 以感觉更好使用 Perl 数据结构和 perldoc -f sort 了解有关 Perl 中排序的更多信息。
祝你好运!
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!