You claim $conf->{group}->{names}
is an array as opposed to a reference to one, but that's impossible. Hashes values are scalars; they can't be arrays. This is why we store references to arrays in hashes.
That $conf->{group}->{names}[1]
works (short for $conf->{group}->{names}->[1]
) indicates $conf->{group}->{names}
it's an a reference to an array.[1]
You assign this reference to the @list
, and therefore populate @list
with this reference. This is why $list[0]
is this reference.
You wish to iterate over the elements of the array. If you had a named array, you would use
for (@array) {
...
}
But you have a reference to an array. There are two syntaxes that can be used to accomplish that.
The circumfix syntax features a block that returns the reference
for (@{ $ref }) { # Or just @$ref when the block contains a simple scalar.
...
}
In your case,
my $ref = $conf->{group}{names}[1];
for (@$ref) {
...
}
or
for (@{ $conf->{group}{names}[1] }) {
...
}
The postfix (aka "arrow") syntax features can be tacked onto an expression.
for ($ref->@*) {
...
}
In your case,
my $ref = $conf->{group}{names}[1];
for ($ref->@*) {
...
}
or
for ($conf->{group}{names}[1]->@*) {
...
}
->@*
requires Perl 5.24+. It's available in Perl 5.20+ by adding both use feature qw( postderef );
and no warnings qw( experimental::postderef );
, or by adding use experimental qw( postderef );
. This is safe because the then-experimental feature was accepted into Perl without change.
See Perl Dereferencing Syntax and the documentation linked by it.
- I'm assuming you're using
use strict;
as you always should.