我的结构看起来像这样(散列的散列):
$VAR1 = {
'Lee2000a' => {
'abstract' => 'Abstract goes here',
'author' => 'Lee, Wenke and Stolfo, Salvatore J'
'title' => 'Data mining approaches for intrusion detection'
'year' => '2000'
},
'Forrest1996' => {
'abstract' => 'Abstract goes here',
'author' => 'Forrest, Stephanie and Hofmeyr, Steven A. and Anil, Somayaji'
'title' => 'Computer immunology'
'year' => '1996'
}
};我想根据三个条件(按此顺序)对此结构进行排序:
第一-根据年份值(1996,2000)第二-根据“外部”(Lee2000a,Forrest1996)结构键第三-根据“内部”结构关键字(摘要,作者,标题,年份)按字母顺序排列。
到目前为止,我有两个代码需要以某种方式组合起来:
I.代码满足第二和第三标准
for $i (sort keys(%bibliography)){
print "$i => ", "\n";
for $j (sort keys ($bibliography{"$i"})){
print "\t $j -> ", $bibliography{"$i"}{"$j"},"\n";
}
} 二、代码满足第一个条件
for $i (sort { ($bibliography{$a}->{year} || 0) <=> ($bibliography{$b}->{year} || 0) } keys %bibliography){
print "$i => ", "\n";
for $j (sort keys ($bibliography{"$i"})){
print "\t $j -> ", $bibliography{"$i"}{"$j"},"\n";
}
}非常感谢
发布于 2012-12-10 21:26:40
要按一些次要条件进行排序,可以使用逻辑OR:
my @sorted = sort {
$a{c1} <=> $b{c1} ||
$a{c2} <=> $b{c2}
} @unsorted此示例将按键c1对@unsorted中的散列进行排序,如果此比较相等,则按键c2对散列进行排序。
出于您的目的,您可以以这种方式组合外部循环的两个排序比较,以便您的排序块将读取:
(($bibliography{$a}->{year} || 0) <=> ($bibliography{$b}->{year} || 0)) ||
($a cmp $b)发布于 2012-12-10 21:35:29
UPDATE添加了一个更简单的版本,它只返回根据排序条件进行排序的输入散列上的键。
第一种方法:返回排序列表
我们希望对散列进行排序,并且排序条件包括键和嵌套在值中的内容。要执行1遍排序,其中每个$a和$b都可以进行比较,可以方便地将哈希转换为一个列表,从而使哈希中的每个键和值都可用。
有点浪费,但很有效:
my @sorted =
sort {
$a->{val}->{year} <=> $b->{val}->{year} || # 1st condition
$a->{key} <=> $b->{key} || # 2nd condition
$a->{val}->{abstract} <=> $b->{val}->{abstract} || # 3rd condition
$a->{val}->{author} <=> $b->{val}->{author} || # (not really sure
$a->{val}->{title} <=> $b->{val}->{title} || # how you wanted this
$a->{val}->{year} <=> $b->{val}->{year}
} map { { val => $biblio{$_}, key => $_ } } keys %biblio;我们正在对Hash进行排序;我们需要一个list作为返回值。在这种情况下,Forrest1996和Lee2000a应该放在哪里呢?我认为将散列转换为散列列表可能更有意义,每个散列列表都有两个属性-一个key和一个val。
因此,排序的返回值将是一个散列列表,如下所示:
@sorted = (
{
'val' => {
'title' => 'Computer immunology',
'author' => 'Forrest, Stephanie and Hofmeyr, Steven A. and Anil, Somayaji',
'abstract' => 'Abstract goes here',
'year' => 1996
},
'key' => 'Forrest1996'
},
{
'val' => {
'title' => 'Data mining approaches for intrusion detection',
'author' => 'Lee, Wenke and Stolfo, Salvatore J',
'abstract' => 'Abstract goes here',
'year' => 2000
},
'key' => 'Lee2000a'
}
)第二种方法:根据排序条件只返回键的列表
我猜,在阅读评论并重新考虑之后,只返回输入散列的键就足够好了,也更轻便了:
my @sorted =
sort {
$biblio{$a}->{year} <=> $biblio{$b}->{year} || # 1st condition
$a <=> $b || # 2nd condition
$biblio{$a}->{abstract} <=> $biblio{$b}->{abstract} || # 3rd condition
$biblio{$a}->{author} <=> $biblio{$b}->{author} # ...and so on
} keys %biblio;..。它就会返回
@sorted = (
'Forrest1996',
'Lee2000a'
);https://stackoverflow.com/questions/13801758
复制相似问题