在 Perl 中如何按二级哈希值排序?

发布于 2024-08-06 08:28:31 字数 350 浏览 10 评论 0原文

my $hash_ref = {
    one   => { val => 1, name => 'one'  },
    three => { val => 3, name => 'three'},
    two   => { val => 2, name => 'two'  },
};

我想对 $hash_ref 进行排序,以便 foreach 可以按

$hash_ref->{$key}->{'val'}

one
two
three

任何建议对它们进行排序?

my $hash_ref = {
    one   => { val => 1, name => 'one'  },
    three => { val => 3, name => 'three'},
    two   => { val => 2, name => 'two'  },
};

I would like to sort $hash_ref such that a foreach would order them by

$hash_ref->{$key}->{'val'}

one
two
three

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

依 靠 2024-08-13 08:28:31

@sorted_list 是对已排序哈希元素的引用的数组:

 @sorted_list = sort { $a->{'val'} <=> $b->{'val'} } values %{$unsorted_hash_ref};

您可以像这样使用它:

#!/usr/bin/perl

my $hash_ref = {
    one   => { val => 1, name => 'one' },
    three => { val => 3, name => 'three' },
    two   => { val => 2, name => 'two' },
};

foreach $elem ( sort { $a->{'val'} <=> $b->{'val'} } values %{$hash_ref} ) {
    print "$elem->{'val'} : $elem->{'name'}\n";
}

输出:

1 : one
2 : two
3 : three

@sorted_list is an array of references to the sorted hash elements:

 @sorted_list = sort { $a->{'val'} <=> $b->{'val'} } values %{$unsorted_hash_ref};

You can use it like so:

#!/usr/bin/perl

my $hash_ref = {
    one   => { val => 1, name => 'one' },
    three => { val => 3, name => 'three' },
    two   => { val => 2, name => 'two' },
};

foreach $elem ( sort { $a->{'val'} <=> $b->{'val'} } values %{$hash_ref} ) {
    print "$elem->{'val'} : $elem->{'name'}\n";
}

Output:

1 : one
2 : two
3 : three
甜宝宝 2024-08-13 08:28:31

哈希表没有任何特定的顺序。但是,您可以对数组中的键进行排序,并使用它来迭代散列:

my $hash_ref = {
    one => { val => 1, name => 'one'},
    three => { val => 3, name => 'three'},
    two => { val => 2, name => 'two'},
};

use strict;
use warnings;
use Lingua::EN::Words2Nums;

foreach my $key (sort { words2nums($a) <=> words2nums($b) } keys %$hash_ref)
{
    # do something with $hash_ref->{$key}
    print "processing key $key.\n";
}

您可以将任何您喜欢的内容定义为排序方法;有关详细信息,请参阅 perldoc -f sort。从序数数字文本到算术值的转换是通过 Lingua 完成的: :EN::Words2Nums (它也可以处理基数)。

Hash tables don't have any specific order. However, you can sort the keys in an array and use that to iterate through the hash:

my $hash_ref = {
    one => { val => 1, name => 'one'},
    three => { val => 3, name => 'three'},
    two => { val => 2, name => 'two'},
};

use strict;
use warnings;
use Lingua::EN::Words2Nums;

foreach my $key (sort { words2nums($a) <=> words2nums($b) } keys %$hash_ref)
{
    # do something with $hash_ref->{$key}
    print "processing key $key.\n";
}

You can define anything you like as a sort method; see perldoc -f sort for more details. Conversion from ordinal numerical text to arithmetic values is done with Lingua::EN::Words2Nums (it does cardinal numbers too).

演出会有结束 2024-08-13 08:28:31
use strict;
use warnings;

my %hash_ref = (
one   => { val => 1, name => 'one'  },
three => { val => 3, name => 'three'},
two   => { val => 2, name => 'two'  },
);

foreach my $key(sort {$hash_ref{$a}{val} <=> $hash_ref{$b}{val}} keys %hash_ref) {
my $value = $hash_ref{$key}{val};
my $name = $hash_ref{$key}{name};
print "$name -> $value\n";
}

output:
one -> 1
two -> 2
three -> 3
use strict;
use warnings;

my %hash_ref = (
one   => { val => 1, name => 'one'  },
three => { val => 3, name => 'three'},
two   => { val => 2, name => 'two'  },
);

foreach my $key(sort {$hash_ref{$a}{val} <=> $hash_ref{$b}{val}} keys %hash_ref) {
my $value = $hash_ref{$key}{val};
my $name = $hash_ref{$key}{name};
print "$name -> $value\n";
}

output:
one -> 1
two -> 2
three -> 3
扮仙女 2024-08-13 08:28:31
#!/usr/bin/perl


my $hash_ref = (
                one   => {val => 1,  name => "one"},
                three => {val => 3,  name => "three"},
                two   => {val => 2,  name => 'two'},
               );

foreach $elem( sort {$hash_ref{$a}{val} <=> $hash_ref{$b}{val}} keys %$hash_ref){
    my $value = $hash_ref->{$elem}{val};
    my $name = $hash_ref->{$elem}{name};
    print "$name -> $value\n";
}

OutPut:
one -> 1
two -> 2
three -> 3
#!/usr/bin/perl


my $hash_ref = (
                one   => {val => 1,  name => "one"},
                three => {val => 3,  name => "three"},
                two   => {val => 2,  name => 'two'},
               );

foreach $elem( sort {$hash_ref{$a}{val} <=> $hash_ref{$b}{val}} keys %$hash_ref){
    my $value = $hash_ref->{$elem}{val};
    my $name = $hash_ref->{$elem}{name};
    print "$name -> $value\n";
}

OutPut:
one -> 1
two -> 2
three -> 3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文