Your code gives me the error
Odd number of elements in hash assignment at ./t4.pl line 6.
Hashes are defined this way:
my %hash = ( somekey => somevalue, ...);
So your code should read:
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my %w = ( Sub =>
{
'rbscells' =>
'UtranCell=RNC16-4-1',
'UtranCell=RNC16-4-2',
'UtranCell=RNC16-4-3'
}
)
;
print Dumper(%w);
Now when you look at the output of
Data::Dumper, you can see, that what you probably wanted to be an array (the three strings starting with "UtranCell*") are in fact a hash value and another hash key + value:
$VAR1 = 'Sub';
$VAR2 = {
'rbscells' => 'UtranCell=RNC16-4-1',
'UtranCell=RNC16-4-2' => 'UtranCell=RNC16-4-3'
};
However, you can't just put an array definition inside a hash - from
perldsc:
The most important thing to understand about all data structures in
Perl--including multidimensional arrays--is that even though they might appear
otherwise, Perl @ARRAYs and %HASHes are all internally one-dimensional. They
can hold only scalar values (meaning a string, number, or a reference). They
cannot directly contain other arrays or hashes, but instead contain references
to other arrays or hashes.
So another change, to define an array reference:
...
'rbscells' => [
'UtranCell=RNC16-4-1',
'UtranCell=RNC16-4-2',
'UtranCell=RNC16-4-3' ,
]
...
You can access the elements of the array as always, via index starting from 0 or with foreach like this:
foreach my $element (@{$w{Sub}->{rbscells}})
{
print $element . "\n";
}
To understand nested data structures in perl, please refer to
perldsc, especially the chapter about "Hashes of Arrays".