Odd number of elements in hash assignment at ./t4.pl line 6.Hashes are defined this way:
So your code should read:my %hash = ( somekey => somevalue, ...);
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:#!/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);
However, you can't just put an array definition inside a hash - from perldsc:$VAR1 = 'Sub'; $VAR2 = { 'rbscells' => 'UtranCell=RNC16-4-1', 'UtranCell=RNC16-4-2' => 'UtranCell=RNC16-4-3' };
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:
You can access the elements of the array as always, via index starting from 0 or with foreach like this:... 'rbscells' => [ 'UtranCell=RNC16-4-1', 'UtranCell=RNC16-4-2', 'UtranCell=RNC16-4-3' , ] ...
To understand nested data structures in perl, please refer to perldsc, especially the chapter about "Hashes of Arrays".foreach my $element (@{$w{Sub}->{rbscells}}) { print $element . "\n"; }
In reply to Re: Usage of hashes with hashes when mulitple values are present
by lune
in thread Usage of hashes with hashes when mulitple values are present
by Bhagya
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |