Perl 如何从数组的散列中检索数组? [英] Perl How do I retrieve an array from a hash of arrays?
问题描述
我对 Perl 还很陌生,所以如果这似乎是一个简单的问题,请原谅我...
I'm fairly new to Perl, so forgive me if this seems like a simple question...
无论如何,我有一个数组散列,我正在尝试检索散列中的一个数组,但我所能得到的只是数组的标量大小.
Anyway, I have a hash of arrays and I'm trying to retrieve one of the arrays in the hash, but all I can get is the scalar size of the array.
%HoA = a hash of arrays
$key = some key in the hash
foreach $nextItem (@HoA{$key}) {
do a bunch of stuff with $nextItem
}
当我这样做时,$nextItem 总是只是数组的大小并且循环只运行一次.我试过打印以下内容:
When I do this, $nextItem is always just the size of the array and the loop only runs through one time. I've tried printing the following:
@HoA{$key}
$HoA{$key}
@$HoA{$key}
前两个给了我标量大小,第三个给了我什么......我在这里错过了什么?
The first two give me the scalar size and the third gives me nothing...what am I missing here?
更新:我想知道我的问题是否真的是我将数组添加到散列的方式.这是我正在做的:
UPDATE: I'm wondering if my problem is actually the way I'm adding the arrays to the hash. Here's what I'm doing:
@HoA{$key} = split(/ /, $list);
这是将数组粘贴在哈希中,还是将数组大小粘贴在哈希中?
Does that stick the array in the hash, or the array size in the hash?
更新 2:我尝试了以下代码块:
UPDATE 2: I tried the following block of code:
my $key = "TEST";
my %HoA = ();
my @testarray = (1, 2, 3);
@HoA{$key} = @testarray;
print Dumper(%HoA);
输出如下:
$VAR1 = 'TEST';
$VAR2 = 1;
为什么只贴数组的第一个值?
Why is it only sticking the first value of the array in?
推荐答案
尝试以这种方式引用您的数组:
Try referencing your array this way:
%HoA = a hash of arrays
$key = some key in the hash
foreach $nextItem (@{$HoA{$key}}) {
do a bunch of stuff with $nextItem
}
您在 $HoA{$key}
处获取数组引用并将其设为数组.
You take the array reference at $HoA{$key}
and make it an array.
对于你的更新,我认为你会得到你想要的,如果你这样做:
For your update, I think you will get what you want, if you do it this way:
@{$HoA{$key}} = split(/ /, $list);
或者你可以做
push(@{$HoA{$key}}, split(/ /, $list);
例如:
my $list = "fred joe brown sam";
my %HoA = ();
my $key = "jacob";
@{$HoA{$key} = split(/ /, $list);
foreach my $item (@{$HoA{$key}})
{
print "Test item: $nextItem\n";
}
You will get:
Test item: fred
Test item: joe
Test item: brown
Test item: sam
将 use strict;
添加到程序的顶部.基本上,当您定义了散列时,您试图将 HoA 用作数组.您不正确地引用了哈希的内容.为了正确地做到这一点,你真的需要在 @
和 HoA
之间有一个 $
.Perl 是无类型的,如果你不use strict;
,它会让你逍遥法外.来自oreilly的参考摘录可能会澄清一些问题.
Add use strict;
to the top of your program. Basically you are attempting to use HoA as an array when you have a hash defined. You are referencing the contents of your hash improperly. To do it properly you really really need to have a $
between the @
and HoA
. Perl is typeless and will let you get away with murder if you don't use strict;
. A reference excerpt from oreilly might clear a few things up.
my @testarray
是一个数组 我的 %hash
是一个哈希值 $hash{$el1} = \@array
是一个哈希元素,它的值是一个数组的引用@{$hash{$el1}} = @array
是一个包含数组的哈希元素
my @testarray
is an array
my %hash
is a hash
$hash{$el1} = \@array
is a hash element that has the value of a ref to an array
@{$hash{$el1}} = @array
is a hash element that contains an array
这篇关于Perl 如何从数组的散列中检索数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!