以下是我设置哈希的方法:
my @keys_i_need = qw(A B C D E F G);
my %keys_i_need = map {$_ => []} @keys_i_need;
foreach my $line (@{$file_arr_ref}) {
my $sub = substr( $line, 0, 1);
if(($sub ne "#") and ($sub ne "")){
my @key_vals = split(/\s+/, $line);
my $key = shift @key_vals;
if(exists $keys_i_need{$key}) {
INFO("key is $key value is " . join(", ", @key_vals));
push (@{$keys_i_need{$key}}, \@key_vals);
DEBUG(Dumper \%keys_i_need);
}
}
}
如果我理解正确,它是一个哈希值,其中每个值都是一个数组引用,数组引用在数组引用中。我不想使用Dumper,因为我想挑出每一件。
我正在尝试读出已被拉入哈希的内容,但我收到一条错误消息:
"my" variable $values masks earlier declaration in same statement at /home/rabdelaz/workspace/akatest_5/scripts/Viper/Stragglers.pl line 67.
foreach my $key (keys %$config_options) {
foreach my $arr_ref_of_arr_values (%$config_options{$key}) {
foreach my $values (@$arr_ref_of_arr_values) { #<----------line 67
foreach my $value (@$values) {
INFO("key $key has values $value");
}
}
}
}
这对我来说是正确的。我无法弄清楚perl在抱怨什么。有什么想法吗?
答案 0 :(得分:0)
我明白我现在做了什么。 由于数组引用是标量而不是数组,我感到困惑(再一次)。虽然,从我的原始代码中可以看出,我的方向是错误的(在哈希之外,而不是在数组引用内部。所以我的最终代码看起来应该更像这样:
foreach my $key (keys %$config_options) {
foreach my $arr_ref_of_arr_values ($$config_options{$key}) {
foreach my $values (@$arr_ref_of_arr_values) { #<----------line 67
foreach my $value (@$values) {
INFO("key $key has values $value");
}
}
}
}
在调试过程中,我最终用这个来获取第一个键中第一个数组的第一个值:
INFO("first value of first array of first key: " . ${${$$config_options{'A'}}[0]}[0]);
然后我用它作为我的指南。