【发布时间】:2013-10-24 04:15:09
【问题描述】:
我有两个如下形式的数组:
root rhino root root root root root root root root root root domainte root
stam rhino jam onetwo domante ftpsi jay testwp contra raul vnod foos raul bruce
使用help I got from SO,我将它们都放入哈希中,如下所示:
my %hash;
for my $idx (0 .. $#test2) {
push @{ $hash{ $test2[$idx] } }, $test3[$idx];}
print "<br /><br /><br /><br />";
print Dumper \%hash;
给出以下输出:
$VAR1 = { 'rhino' => [ 'rhino' ],
'domante' => [ 'raul' ],
'root' => [ 'stam', 'jam', 'onetwo', 'domante', 'ftpsi',
'jay', 'testwp', 'contra', 'raul', 'vnod',
'foos', 'bruce' ]
};
现在将键和值推送到 2 个数组,如下所示:
my @q1 = keys %hash;
my @q2 = values %hash;
print "<br /><br /><br /><br />";
print @q1;
print "<br /><br /><br /><br />";
print @q2;
在打印时,我得到了正确的键,但值会打印以下输出:
ARRAY(0x9bf0b0)ARRAY(0x9bf1e8)ARRAY(0x9bf068)
如何将所有值放入数组?我做错了什么?
编辑:
这是我尝试过的:
foreach (@q1)
{ print @{$hash{$q1}};
print "<br />";
}
但没有得到可行的结果。
【问题讨论】:
-
你的哈希是叫
%hash还是叫%owners? -
@mob - %hash - 这是一个错字。已更正。对不起
-
你没有得到可行的结果,因为
$_是foreach循环的索引,而不是$q1。 (你在用strict和warnings吗?)