[英]How do I store a hash which is inside an array element?
I got a Perl module which utilizes an array for its input/output parameters, like this: 我得到了一个Perl模块,该模块利用数组作为输入/输出参数,如下所示:
Execute({inputfile => $req->{modules}.'filename', param => \@xchange});
Inside the module a hash is build and returned via reference 在模块内部构建哈希并通过引用返回
$param[0] = \%values;
This is all fine and good (I think) and print Dumper @xchange[0];
一切都很好(我认为),并
print Dumper @xchange[0];
will output my desired content as 会将我想要的内容输出为
$VAR1 = { '33' => 'Title1', '53' => 'Title2', '21' => 'Title3' };
I would like to loop over the content and print the key/value pairs one by one, for example like this 我想遍历内容并逐个打印键/值对,例如这样
%testhash = ('33' => 'Test1', '53' => 'Test2', '21' => 'Test3' );
foreach $key (keys %testhash) {
print "LOOP: $key, value=$testhash{$key}\n";
}
This loop does work as intended and dumping my testhash via print Dumper \\%testhash;
这个循环确实按预期工作,并通过
print Dumper \\%testhash;
转储我的print Dumper \\%testhash;
outputs the same as the array element above 输出与上面的数组元素相同
$VAR1 = { '33' => 'Test1', '53' => 'Test2', '21' => 'Test3' };
The trouble now seems to be that although both structures appear to be of the same kind I cant get my head arround, how to properly access the returned hash which is stored inside @xchange[0]. 现在的麻烦似乎是,尽管这两种结构看起来都是同一种类型,我无法理解,但是如何正确访问存储在@xchange [0]中的返回哈希值。
I did try %realhash = @xchange[0];
我确实尝试了
%realhash = @xchange[0];
and %realhash = \\@xchange[0];
和
%realhash = \\@xchange[0];
, but then print Dumper \\%realhash;
,然后
print Dumper \\%realhash;
will output $VAR1 = { 'HASH(0xa7b29c0)' => undef };
将输出
$VAR1 = { 'HASH(0xa7b29c0)' => undef };
or $VAR1 = { 'REF(0xa7833a0)' => undef };
或
$VAR1 = { 'REF(0xa7833a0)' => undef };
respectively. 分别。
So I either need a way to get the content of @xchange[0] inside a clean new hash or a way to foreach loop over the hash inside the @xchange[0] element. 因此,我要么需要一种方法来获取干净的新哈希中@xchange [0]的内容,要么需要一种方法来遍历@xchange [0]元素内部的哈希。
I guess I am getting screwed by the whole hash reference concept, but I am at a loss here and can't think of another way to google for it. 我想我已经为整个哈希引用概念所困扰,但是我在这里很茫然,无法想到另一种使用Google的方法。
$xchange[0]
is a hash reference. $xchange[0]
是一个哈希引用。 Use the dereference operator %{...}
to access it as a hash. 使用取消引用运算符
%{...}
将其作为哈希访问。
%realhash = %{$xchange[0]};
@xchange[0]
is a scalar value, it contains the reference to a hash. @xchange[0]
是标量值,它包含对哈希的引用。 When you assign it to a hash 当您将其分配给哈希时
%hash = @xchange[0];
The reference is stringified into something like HASH(0xa7b29c0)
, and you get the warnings 该引用被字符串化为
HASH(0xa7b29c0)
,您会收到警告
Scalar value @xchange[0] better written as $xchange[0] at ...
Reference found where even-sized list expected at ...
That is to say, you get these warnings, unless you have been so foolish as to not turn warnings on with use warnings
. 这就是说,您会收到这些警告, 除非您如此愚蠢以至于无法
use warnings
来打开use warnings
。
The first one means what it says. 第一个表示它的意思。 The second one means that the list you assign to a hash should have an even number of elements: one value for every key.
第二个意味着您分配给哈希的列表应该具有偶数个元素:每个键一个值。 You only passed a "key" (something that Perl took as a key).
您只传递了一个“密钥”(Perl作为密钥)。 The value then becomes undef, as noted in your
Data::Dumper
output: 然后该值变为undef,如您的
Data::Dumper
输出中所述:
$VAR1 = { 'HASH(0xa7b29c0)' => undef }
What you need to do is dereference the reference. 您需要做的是取消引用的引用。
my $href = $xchange[0];
my %hash = %$href; # using a transition variable
my %hash2 = %{ $xchange[0] } # using support curly braces
use warnings;
use strict;
use Data::Dumper;
$Data::Dumper::Sortkeys=1;
my %testhash = ('33' => 'Test1', '53' => 'Test2', '21' => 'Test3' );
# Add hash as first element of xchange AoH
my @xchange = \%testhash;
# Derefererence 1st element of AoH as a hash
my %realhash = %{ $xchange[0] };
# Dump new hash
print Dumper(\%realhash);
__END__
$VAR1 = {
'21' => 'Test3',
'33' => 'Test1',
'53' => 'Test2'
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.