我有一个数组(perl)的散列,其键是一个由制表符连接的字符串。这就是哈希键的样子
chr"\t"fivep"\t"threep"\t"strand # separated on tab
Run Code Online (Sandbox Code Playgroud)
如果哈希被命名 %output
我想对这个散列的键进行排序,这样首先排序是在 chr 上完成的,然后是 Fivep,然后是 Threep。
我尝试了以下代码进行排序:
foreach my $k(sort keys %output){
print join("\t",$k,@{$output{$k}}),"\n";
}
Run Code Online (Sandbox Code Playgroud)
这种排序只对 chr 进行排序,但我想在它之后排序五个,然后再排序三个。
我怎样才能做到这一点?
我将建议您对键进行转换,然后进行自定义排序。
for my $k (
map { $_->[0] } # pull out the original key
sort { $a->[1] cmp $b->[1] || $a->[2] cmp $b->[2] || $a->[3] cmp $b->[3] } # do the actual sort
map { [ $_, split /\t/, $_, -1 ] } # split the keys and make the transform
keys %output
) {
print join "\t", $k, @output{$k};
}
Run Code Online (Sandbox Code Playgroud)
如果sort代码块过于复杂,或者该过程需要在代码中的更多地方重用,您可以将代码块分解为它自己的函数,然后只需为sort.
| 归档时间: |
|
| 查看次数: |
118 次 |
| 最近记录: |