How do I create an array of hashes and loop through them in Perl?

时间:2022-04-11 07:08:48

I'm trying to create an array of hashes, but I'm having trouble looping through the array. I have tried this code, but it does not work:

我正在尝试创建一个哈希数组,但是我在循环数组时遇到了麻烦。我试过这段代码,但它不起作用:

for  ($i = 0; $i<@pattern; $i++){
  while(($k, $v)= each $pattern[$i]){
    debug(" $k: $v");
  }
}

3 个解决方案

#1


24  

First, why aren't you useing strict and warnings? The following lines should be at the top of every Perl program you create, right after #!/usr/bin/perl. Always.

首先,你为什么不使用严格和警告?以下几行应位于您创建的每个Perl程序的顶部,紧跟在#!/ usr / bin / perl之后。总是。

use strict;
use warnings;

And I know you aren't because I'm pretty sure you'd get some nice error messages out of strict and warnings from this, and from many other places in your code as well, judging by your variable use.

而且我知道你不是因为我很确定你会得到一些很好的错误信息,这些错误信息来自严格和警告,以及代码中的许多其他地方,从你的变量使用来判断。

Second, why aren't you doing this:

第二,你为什么不这样做:

for my $i (@pattern) {
  ..
}

That loops through every element in @pattern, assigning them to $i one at a time. Then, in your loop, when you want a particular element, just use $i. Changes to $i will be reflected in @pattern, and when the loop exits, $i will fall out of scope, essentially cleaning up after itself.

它遍历@pattern中的每个元素,一次将它们分配给$ i。然后,在你的循环中,当你想要一个特定元素时,只需使用$ i。对$ i的更改将反映在@pattern中,当循环退出时,$ i将超出范围,基本上清理后自身。

Third, for the love of Larry Wall, please declare your variables with my to localize them. It's really not that hard, and it makes you a better person, I promise.

第三,对于Larry Wall的爱,请用我的变量声明你的变量来进行本地化。我保证,这真的不是那么难,它会让你成为一个更好的人。

Fourth, and last, your array stores references to hashes, not hashes. If they stored hashes, your code would be wrong because hashes start with %, not $. As it is, references (of any kind) are scalar values, and thus start with $. So we need to dereference them to get hashes:

第四,也就是说,你的数组存储对哈希的引用,而不是哈希。如果它们存储了哈希值,那么代码就会出错,因为哈希值以%开头,而不是$。实际上,引用(任何类型)都是标量值,因此以$开头。因此我们需要取消引用它们以获得哈希:

for my $i (@pattern) {
  while(my($k, $v) = each %{$i}) {
    debug(" $k: $v");
  }
}

Or, your way:

或者,你的方式:

for  (my $i = 0; $i<@pattern; $i++) { # added a my() for good measure
  while(my($k, $v) = each %{$pattern[$i]}) {
    debug(" $k: $v");
  }
}

#2


6  

Try this instead:

试试这个:

for my $hashref (@pattern) {
    for my $key (keys %$hashref) {
        debug "$key: $hashref->{$key}";
    }
}

The biggest problem with what you were trying was each $pattern[$i]. The each function expects a hash to work on, but $pattern[$i] returns a hashref (i.e. a reference to a hash). You could fix your code by dereferencing $pattern[$i] as a hash:

你尝试的最大问题是每个$ pattern [$ i]。每个函数都需要哈希处理,但$ pattern [$ i]返回一个hashref(即对哈希的引用)。您可以通过取消引用$ pattern [$ i]作为哈希来修复代码:

while(my($k, $v) = each %{$pattern[$i]}) {

Also, beware of the each function, it can leave the hash iterator in an incomplete state.

另外,要注意每个函数,它可以使散列迭代器处于不完整状态。

#3


4  

See the documentation for the perl data structures cookbook: perldoc perldsc

请参阅perl数据结构cookbook的文档:perldoc perldsc

#1


24  

First, why aren't you useing strict and warnings? The following lines should be at the top of every Perl program you create, right after #!/usr/bin/perl. Always.

首先,你为什么不使用严格和警告?以下几行应位于您创建的每个Perl程序的顶部,紧跟在#!/ usr / bin / perl之后。总是。

use strict;
use warnings;

And I know you aren't because I'm pretty sure you'd get some nice error messages out of strict and warnings from this, and from many other places in your code as well, judging by your variable use.

而且我知道你不是因为我很确定你会得到一些很好的错误信息,这些错误信息来自严格和警告,以及代码中的许多其他地方,从你的变量使用来判断。

Second, why aren't you doing this:

第二,你为什么不这样做:

for my $i (@pattern) {
  ..
}

That loops through every element in @pattern, assigning them to $i one at a time. Then, in your loop, when you want a particular element, just use $i. Changes to $i will be reflected in @pattern, and when the loop exits, $i will fall out of scope, essentially cleaning up after itself.

它遍历@pattern中的每个元素,一次将它们分配给$ i。然后,在你的循环中,当你想要一个特定元素时,只需使用$ i。对$ i的更改将反映在@pattern中,当循环退出时,$ i将超出范围,基本上清理后自身。

Third, for the love of Larry Wall, please declare your variables with my to localize them. It's really not that hard, and it makes you a better person, I promise.

第三,对于Larry Wall的爱,请用我的变量声明你的变量来进行本地化。我保证,这真的不是那么难,它会让你成为一个更好的人。

Fourth, and last, your array stores references to hashes, not hashes. If they stored hashes, your code would be wrong because hashes start with %, not $. As it is, references (of any kind) are scalar values, and thus start with $. So we need to dereference them to get hashes:

第四,也就是说,你的数组存储对哈希的引用,而不是哈希。如果它们存储了哈希值,那么代码就会出错,因为哈希值以%开头,而不是$。实际上,引用(任何类型)都是标量值,因此以$开头。因此我们需要取消引用它们以获得哈希:

for my $i (@pattern) {
  while(my($k, $v) = each %{$i}) {
    debug(" $k: $v");
  }
}

Or, your way:

或者,你的方式:

for  (my $i = 0; $i<@pattern; $i++) { # added a my() for good measure
  while(my($k, $v) = each %{$pattern[$i]}) {
    debug(" $k: $v");
  }
}

#2


6  

Try this instead:

试试这个:

for my $hashref (@pattern) {
    for my $key (keys %$hashref) {
        debug "$key: $hashref->{$key}";
    }
}

The biggest problem with what you were trying was each $pattern[$i]. The each function expects a hash to work on, but $pattern[$i] returns a hashref (i.e. a reference to a hash). You could fix your code by dereferencing $pattern[$i] as a hash:

你尝试的最大问题是每个$ pattern [$ i]。每个函数都需要哈希处理,但$ pattern [$ i]返回一个hashref(即对哈希的引用)。您可以通过取消引用$ pattern [$ i]作为哈希来修复代码:

while(my($k, $v) = each %{$pattern[$i]}) {

Also, beware of the each function, it can leave the hash iterator in an incomplete state.

另外,要注意每个函数,它可以使散列迭代器处于不完整状态。

#3


4  

See the documentation for the perl data structures cookbook: perldoc perldsc

请参阅perl数据结构cookbook的文档:perldoc perldsc