简体   繁体   中英

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:

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]; 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; 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].

I did try %realhash = @xchange[0]; and %realhash = \\@xchange[0]; , but then print Dumper \\%realhash; will output $VAR1 = { 'HASH(0xa7b29c0)' => undef }; or $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.

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.

$xchange[0] is a hash reference. 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. When you assign it to a hash

%hash = @xchange[0];

The reference is stringified into something like HASH(0xa7b29c0) , and you get the warnings

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 .

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). The value then becomes undef, as noted in your Data::Dumper output:

$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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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