gpt4 book ai didi

perl - 在 perl 中对哈希键进行排序,键是制表符分隔的

转载 作者:行者123 更新时间:2023-12-04 00:00:08 25 4
gpt4 key购买 nike

我有一个数组哈希(perl),其键是一个由制表符连接的字符串。这就是哈希键的样子

chr"\t"fivep"\t"threep"\t"strand  # separated on tab

如果哈希被命名为 %output

我想对这个哈希的键进行排序,这样首先排序在 chr 上完成,然后在 Fivep 上,然后在 threep 上。

我尝试了以下代码进行排序:

foreach my $k(sort keys %output){
print join("\t",$k,@{$output{$k}}),"\n";
}

这仅对 chr 进行排序,但我想在其后排序 5p,然后再排序 3p。

我该怎么做?

最佳答案

我将建议您对键进行转换,然后进行自定义排序。

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};
}

如果 sort 代码块过于复杂或者这个过程也需要在代码中的更多地方重用,你可以将 sort 代码块分解到它自己的函数中,然后给 函数>排序.

关于perl - 在 perl 中对哈希键进行排序,键是制表符分隔的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63001867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com