简体   繁体   English

迭代perl中的哈希值

[英]Iterate through hash values in perl

I've got a hash with let's say 20 values. 我有一个哈希,让我们说20个值。

It's initialized this way: 它以这种方式初始化:

my $line = $_[0]->[0];

    foreach my $value ($line) {
        print $value;
    }

Now when I try to get the value of each hash in $line it says: Use of uninitialized value in print at file.pl line 89 现在,当我尝试获取$ line中每个哈希的值时,它表示:在file.pl第89行打印时使用未初始化的值

Is there a way to iterate through each value of a hash? 有没有办法迭代哈希的每个值?

I also tried it with: 我也尝试过:

my %line = $_[0]->[0];

    foreach my $key (keys %line) {
        print %line->{$key};
    }

But that is also not working: Reference found where even-sized list expected at file.pl at line 89 但这也行不通:参考文献找到了第89行的file.pl所需的偶数大小列表

Anybody knows what to do? 谁知道该怎么办? It shouldn't be that difficult... 应该不那么困难......

To iterate over values in a hash: 迭代哈希中的值:

for my $value (values %hash) {
  print $value;
}

$line in your first example is a scalar, not a hash. 第一个示例中的$line是标量,而不是哈希。

If it's a hash reference, dereference it with %{$line} . 如果它是哈希引用,请使用%{$line}取消引用它。

First, you must understand the difference between a hash, and a hash reference. 首先,您必须了解散列和散列引用之间的区别。

Your initial assignment $_[0]->[0] means something like : Takes the first argument of the current function ( $_[0] ), dereference it ( -> ) and consider it is an array and retrieves it's first value ( [0] ). 你的初始赋值$_[0]->[0]意味着:取当前函数的第一个参数( $_[0] ),取消引用它( -> )并认为它是一个数组并检索它的第一个值( [0] )。 That value can not be a list or a hash, it must be a scalar (string, int, float, reference). 该值不能是列表或散列,它必须是标量(string,int,float,reference)。

Here is some example: 这是一些例子:

my %hash = ( MyKey => "MyValue");
my $hashref = \%hash;
# The next line print all elements of %hash
foreach (keys %hash) { print $_ }
# And is equivalent to
foreach (keys %{$hashref}) { print $_ }
$hash{MyKey} == $hashref->{MyKey}; # is true

Please refer to http://perldoc.perl.org/perlreftut.html for further details. 有关更多详细信息,请参阅http://perldoc.perl.org/perlreftut.html

The warning is telling you that there nothing at $_[0]->[0] . 警告告诉你$_[0]->[0] 没有任何内容 It's not dying and telling you that you're indexing nothing, so $_[0] is likely an arrayref, but nothing is in the first slot--or perhaps it's pointing to an empty array. 它并没有死,并告诉你你没有索引,所以$_[0]可能是一个arrayref,但没有任何东西在第一个插槽 - 或者它可能指向一个空数组。

  • Were it a empty string or a 0, it wouldn't complain. 如果它是一个空字符串或0,它不会抱怨。
  • Were there any reference there, you could print something even if only: BLAH(0x80af74) . 如果有任何参考,你可以打印一些东西,即使只有: BLAH(0x80af74) (Where "BLAH" is one of "ARRAY", "HASH", "SCALAR", "REF", "GLOB", "IO", ... ) (其中“BLAH”是“ARRAY”,“HASH”,“SCALAR”,“REF”,“GLOB”,“IO”之一,......)

My suggestion is that you do this: 我的建议是你这样做:

use Data::Dumper;
say Data::Dumper->Dump( [ $_[0] ] ); # or even say Data::Dumper->Dump( [ \@_ ] )

and then look at the output. 然后查看输出。

Once you've got a hashref at $_[0]->[0] , then if you must loop through the hash, the best way is: 一旦你在$_[0]->[0]得到一个hashref,那么如果你必须循环遍历哈希,最好的方法是:

while ( my ( $key, $value ) = each %$hashref ) { 
   do_stuff_with_key_and_value( $key, $value );
}

see each 看到each

Lastly, it seems that you have some sigil confusion. 最后,似乎你有一些印记混乱。 See the last part of this link for a decent attempt to explain that sigils ( '$' , '@' , '%' ) are not part of the name of a variable, but indicators what we want retrieved from it. 请参阅此链接的最后一部分,以获得一个体面的尝试来解释sigils( '$''@''%'不是变量名称的一部分,而是我们想要从中检索的指标。 Perl compilation woes Perl编译困境

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM