简体   繁体   English

如何存储数组元素内部的哈希?

[英]How do I store a hash which is inside an array element?

The background 的背景

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' };


The goal 目标

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 problem 问题

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

perldsc Perldsc

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.

相关问题 如何初始化位于 AngularJS 中另一个数组元素内的数组元素? - How do I initialize an array element which is inside another array element in AngularJS? 如何访问ruby对象内部数组中的哈希? - How do I access a hash inside an array inside a ruby object? 如何将某些值存储在新数组中的 JSON 散列中? - How do I store certain values in a JSON hash in a new array? 我如何在存储在哈希中的数组内搜索字符串值 - how do I search for a string value inside an array stored in a hash 如何遍历 Raku 哈希中的数组? - How do I iterate through an array inside a Raku hash? 如何在Ruby中访问哈希内的数组元素 - How to access an array element inside a hash in Ruby 如何访问 Javascript 中数组内部的元素? - How do I access an element inside of an array that is inside of an array in Javascript? 如何更新哈希数组中的值,该数组位于 perl 的 hash 的 hash 中? - How do I update values in an array of hashes, which is in a hash of a hash in perl? 如何将令牌存储到给定数组中它们自己的元素中? - How do I store tokens into their own element in a given array? angularjs - 如何在 sessionStorage 中存储数组? - angularjs - How do I store an array inside sessionStorage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM