如何排序其值为数组引用的Perl哈希值? [英] How can I sort Perl hashes whose values are array references?

查看:200
本文介绍了如何排序其值为数组引用的Perl哈希值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我只是想知道是否有一个很酷的单线程,将我的哈希排序保存数组引用。所以我有一堆键/值在我的哈希是这样的:

  $ DataBase {$ key} = \ @ value ; 

然而,我想按数组[0]对散列进行排序元素。然后通过他们循环。我有这个开始:

$ foreach $ key $($ DataBase {$ a} cmp $ DataBase {$ b }}键%DataBase)

但这显然只是将数组的指针值排序。它并不一定是一条线,但我希望有一个解决方案,不涉及重构哈希。

解决方案

  foreach my $ key(sort {$ DataBase {$ a}  - > [0] cmp $ DataBase {$ b}  - > [0]} keys%DataBase)


Hey I was just wondering if there is a cool "one liner" that would sort my hash holding array references. So I have a bunch of key/values in my hash something like:

$DataBase{$key} = \@value;

However I would like to sort the hash by the array[0] element. Then loop through 'em. I had this to begin with:

foreach my $key (sort {$DataBase{$a} cmp $DataBase{$b} } keys %DataBase)

But that obviously just sorts my hash by the pointer value of the array. It doesn't exactly have to be "one line" but I was hoping for a solution that didn't involve reconstructing the hash.

解决方案

foreach my $key (sort {$DataBase{$a}->[0] cmp $DataBase{$b}->[0] } keys %DataBase)

这篇关于如何排序其值为数组引用的Perl哈希值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆