Reputation: 119
Given a HoHoA
my %FIELDS = (
LA => {
NAME => [1],
ADDRESS => [2,3,4,5],
TYPE => [6],
LICENCE => [0],
ACTIVE => [],
},
...
);
I'm trying to make a copy of a particular array
my @ADDRESS_FIELDS = @{$FIELDS{$STATE}->{ADDRESS} };
Since everything inside %FIELDS is by reference the arrow de-references the inner hash and the @{} de-references the array. (I understand that the arrow isn't strictly necessary)
print $ADDRESS_FIELDS[3]."\n";
print @ADDRESS_FIELDS."\n";
gives
5
4
The first print behaves as expected but the second one is giving me the scalar value of, I assume, the referenced array instead of a new copy. Where am I going astray?
Upvotes: 1
Views: 108
Reputation: 694
cat my.pl
#!/bin/perl
#
use strict;
use warnings;
use Data::Dumper;
my %FIELDS = (
LA => {
NAME => [1],
ADDRESS => [2,3,4,5],
TYPE => [6],
LICENCE => [0],
ACTIVE => []
},
);
my @addy = @{$FIELDS{LA}->{ADDRESS}};
foreach my $i(@addy){
print "i=$i\n";
}
perl my.pl
i=2
i=3
i=4
i=5
Upvotes: 1
Reputation: 241988
The concatenation operator forces scalar context on its operands. Use a comma instead:
print @array, "\n";
Note that the elements are separated by $,
, which is empty by default.
Or, to separate the array elements by $"
(space by default) in the output, use
print "@array\n";
Or, join them yourself:
print join(' ', @array), "\n";
Upvotes: 4
Reputation: 2341
print @ADDRESS_FIELDS."\n";
Evaluates your array in a scalar context and returns the number of elements (which in your case is 4).
print join(', ', @ADDRESS_FIELDS)."\n";
is what you want i guess.
HTH
Upvotes: 0