[英]Double hash values in Perl
When using hashes, Perl has the very handy values
keyword to handle only the values of the hash. 使用哈希时,Perl有非常方便的
values
关键字来处理哈希值。
Say I have a double hash, ie something created like this: 假设我有一个双哈希,即像这样创建的东西:
my %dh = map { {$_->{id1}}{$_->{id2}} => $_ } @arr;
Then values %dh
returns an array of hashes. 然后
values %dh
返回哈希数组。 To access the values I would need something like this: 要访问值,我需要这样的东西:
for my $key1 (keys %dh) {
for my $val (values %{ $dh{$key1} }) {
# stuff...
}
}
Is there a better way to do this, avoiding for
cycles? 有没有更好的方式来做到这一点,避免
for
循环? Something like values %{ values %dh }
. 像
values %{ values %dh }
。
Also, sorry for the poor syntax, I'm quite new to Perl. 此外,抱歉语法不好,我对Perl很新。
You can use map
again to extract it. 您可以再次使用
map
来提取它。 Don't be under any illusions though - it's still looping. 不要有任何幻想 - 它仍然在循环。 It might be more elegant though:
它可能更优雅:
for my $val ( map { values %$_ } values %dh ) {
#do stuff
}
This is more or less doing the same thing though - it's flattening your hash - getting the values
from %dh
- which will be a list of hash-refs - and then pulling the values
out of those. 这或多或少做了同样的事情 - 它会使你的哈希变平 - 从
%dh
获取values
- 这将是一个哈希引用列表 - 然后将values
拉出来。 (I think this should do it, but without test data I can't tell you for sure). (我认为应该这样做,但没有测试数据,我无法确定)。
Note - the return order from values
is undefined, so you'll get them in a randomish order. 注意 -
values
的返回顺序是未定义的,因此您将以随机顺序获取它们。 You can control ordering via keys
(and maybe sort
) if you're so inclined. 如果您愿意,可以通过
keys
控制排序(也许可以sort
)。
What map
does is simply evaluate a code block, and returns the results as a list. map
作用是简单地评估代码块,并将结果作为列表返回。 That's why map { $_ => 1 } qw ( one two three )
works - it's actually returning a pair of values, which when you assign it to a hash is treated as key-value pairs. 这就是为什么
map { $_ => 1 } qw ( one two three )
有效 - 它实际上返回一对值, 当你将它分配给一个哈希时,它被视为键值对。
use Data::Dumper;
my @stuff = map { $_ => 1 } qw ( one two three );
print Dumper \@stuff;
my %hash = map { $_ => 1 } qw ( one two three );
print Dumper \%hash;
So - map
is returning 2 values here, which you could write as: 所以 -
map
在这里返回2个值,你可以写成:
my @stuff = map { $_, 1 } qw ( one two three );
Because of course, =>
is really just a comma (with some extra functionality around quoting). 因为当然,
=>
实际上只是一个逗号(引用一些额外的功能)。
So when doing a dereference of an array, you can do the same - values
returns a list of values from each element in the map
. 因此,在对数组进行解引用时,您可以执行相同的操作 -
values
返回map
每个元素的值列表。
For the sake of clarity: 为清楚起见:
use Data::Dumper;
my @stuff = ( [ 1, 2, 3 ], [4, 5, 6], [7, 8, 9] );
print Dumper \@stuff;
my @newstuff = map { @$_ } @stuff;
print Dumper \@newstuff;
Is doing essentially the same thing - traversing each of the hash references in @stuff
(because that's how multi-dimensional arrays are implemented in perl
) and then 'unpacks' them, returning the results. 基本上做同样的事情 - 遍历
@stuff
每个哈希引用(因为这是在perl
中实现多维数组的方式),然后“解包”它们,返回结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.