我对Perl很新,所以请原谅我,如果这看起来像一个简单的问题......
无论如何,我有一个数组哈希,我正在尝试检索哈希中的一个数组,但我能得到的只是数组的标量大小.
%HoA = a hash of arrays
$key = some key in the hash
foreach $nextItem (@HoA{$key}) {
do a bunch of stuff with $nextItem
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,$ nextItem总是只是数组的大小,循环只运行一次.我试过打印以下内容:
@HoA{$key}
$HoA{$key}
@$HoA{$key}
Run Code Online (Sandbox Code Playgroud)
前两个给我标量大小,第三个给了我什么......我在这里缺少什么?
更新:我想知道我的问题是否实际上是我将数组添加到哈希中的方式.这是我正在做的事情:
@HoA{$key} = split(/ /, $list);
Run Code Online (Sandbox Code Playgroud)
这会将数组粘贴在散列中,还是哈希中的数组大小?
更新2:我尝试了以下代码块:
my $key = "TEST";
my %HoA = ();
my @testarray = (1, 2, 3);
@HoA{$key} = @testarray;
print Dumper(%HoA);
Run Code Online (Sandbox Code Playgroud)
这是输出:
$VAR1 = 'TEST';
$VAR2 = 1;
Run Code Online (Sandbox Code Playgroud)
为什么它只是坚持数组的第一个值?
scr*_*ola 16
尝试以这种方式引用您的数组:
%HoA = a hash of arrays
$key = some key in the hash
foreach $nextItem (@{$HoA{$key}}) {
do a bunch of stuff with $nextItem
}
Run Code Online (Sandbox Code Playgroud)
您获取数组引用$HoA{$key}
并使其成为一个数组.
编辑:对于您的更新,我认为如果您这样做,我会得到您想要的:
@{$HoA{$key}} = split(/ /, $list);
Run Code Online (Sandbox Code Playgroud)
或者你可以做
push(@{$HoA{$key}}, split(/ /, $list);
Run Code Online (Sandbox Code Playgroud)
例如:
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
Run Code Online (Sandbox Code Playgroud)
编辑:添加use strict;
到程序的顶部.基本上,当您定义了哈希时,您尝试将HoA用作数组.您正在不正确地引用哈希的内容.要做得恰当,你真的需要$
在@
和之间HoA
.Perl是无类型的,如果你不这样做,它会让你逃脱谋杀use strict;
.来自oreilly的参考摘录可能会清楚一些事情.
my @testarray
是一个数组
my %hash
是一个哈希
$hash{$el1} = \@array
是一个哈希元素,它具有一个引用数组
@{$hash{$el1}} = @array
的值是一个包含数组的哈希元素
Dav*_* W. 11
散列中的每个条目都是对数组的引用.例如:
$my_hash{$key}
Run Code Online (Sandbox Code Playgroud)
是对数组的引用而不是数组.$my_hash{$key}
仅指向该阵列所在的内存区域.要取消引用它,您将数组符号放在前面:
@{ my_hash{$key} }
Run Code Online (Sandbox Code Playgroud)
当我们开始谈论这些元素时,事情变得有点毛茸茸:
${ my_hash{$key} }[0]
Run Code Online (Sandbox Code Playgroud)
是该数组的第一个元素.你可以想象如果那个数组由其他数组的哈希组成,那么语法就会非常明显.幸运的是,Perl有一个干净的处理方式:
$my_hash{$key}->[0];
Run Code Online (Sandbox Code Playgroud)
这与上面完全相同,但更容易理解语法.有时,使用中间变量更容易,因此您可以在没有所有解除引用的情况下引用内容:
my %hash_of_array_refs;
foreach my $key (sort keys %hash_of_array_refs) {
my @array = @{ $hash_of_array_refs{$key} }; #Dereference
foreach my $element (@array) { #Much easier to read and understand
say "First element is $array[0]";
say "last element is $array[$#array]";
say "This element: $element";
}
}
Run Code Online (Sandbox Code Playgroud)
没有解除引用你会得到这个:
my %hash_of_array_refs;
foreach my $key (sort keys %hash_of_array_refs) {
foreach my $element (@{ $hash_of_array_refs{$key} } ) { #Much easier to read and understand
say "First element is " . $hash_of_array_refs->[0];
say "last element is " . $hash_of_array_refs->[$#{ $hash_of_array_refs{key} } ]";
say "This element: $element";
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35615 次 |
最近记录: |