perl的 - 打印散列变量值给出阵列(XXXXXXX) [英] perl - printing hash variable values gives ARRAY(xxxxxxx)

查看:148
本文介绍了perl的 - 打印散列变量值给出阵列(XXXXXXX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下形式的两个数组:

I have two arrays of the following form:

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

使用帮助我从这么了,我已经把两个他们变成像这样的哈希:

Using help I got from SO, I have put both of them into a hash like so:

my %hash;
for my $idx (0 .. $#test2) {
push @{ $hash{ $test2[$idx] } }, $test3[$idx];}
print "<br /><br /><br /><br />";
print Dumper \%hash;

这给下面的输出:

which gives the following output:

$VAR1 = { 'rhino' => [ 'rhino' ], 
          'domante' => [ 'raul' ],
          'root' => [ 'stam', 'jam', 'onetwo', 'domante', 'ftpsi', 
                      'jay', 'testwp', 'contra', 'raul', 'vnod', 
                      'foos', 'bruce' ] 
        };

现在推键和值2数组,像这样:

Now push the key and values to 2 arrays like so:

my @q1 = keys %hash;
 my @q2 = values %hash;

 print "<br /><br /><br /><br />";
 print @q1;
 print "<br /><br /><br /><br />";
 print @q2;

打印时,我拿到钥匙权,但值打印输出如下:

While printing, I get the keys right, but the values print the following output:

ARRAY(0x9bf0b0)ARRAY(0x9bf1e8)ARRAY(0x9bf068)

我如何得到所有的值到数组?我在做什么错了?

How do I get all the values into arrays? What am I doing wrong?

编辑:

下面是我的尝试:

foreach (@q1)
{       print @{$hash{$q1}};
        print "<br />";
}

但没有得到可行的结果。

but got no viable result.

推荐答案

您的哈希值都数组引用的。您可以从数据::自卸车告诉输出,因为所有的值都包含在 [...] 括号。要打印出数组的内容,则需要取消引用数组引用。

The values of your hash are all array references. You can tell from the Data::Dumper output because all the values are enclosed in [ ... ] brackets. To print out the array's contents, you'll need to dereference the array references.

有很多方法可以做到这一点。下面是你可以为你的需要修改一个简洁的方式:

There are a lot of ways to do that. Here's a concise way that you can modify for your needs:

print "@$_\n" for @q2;

$ _ 是一个别名的@ Q2 ,你还记得一个元素是一个数组引用。这位前pression @ $ _ 取消引用参考,返回数组。把 @ $ _ 在双引号将打印数组的每个元素与元素之间的空间。

$_ is an alias to an element of @q2, which you'll recall is an array reference. The expression @$_ dereferences the reference, returning the array. Putting @$_ in double quotes will print every element of the array with a space between the elements.

这篇关于perl的 - 打印散列变量值给出阵列(XXXXXXX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆