Hello, new to Perl and have a little issue about printing and using references with regards to nested hashes and arrays.
I currently have a database that's an array inside several hashes (references) all inside a "real" hash. It looks like this:
my %hash = (
'paramterLevelOne' => {
'paramaterLevelTwo' => {
'parameterLevelThree'
=>'Item1', 'Item2', 'Item3', 'Item4'],
What I'm trying to do is print the whole array at the end like this:
@names = $hash{$parameterLevelOne}{$parameterLevelTwo}{$parameterLevelThree};
foreach (@names) {
print @$_;
}
This prints out all the items fine EXCEPT as one single string, not four.
In order to print all four items as separate list elements I have to write all this junk:
@names = ($hash{$parameterLevelOne}->{$parameterLevelTwo}->{$parameterLevelThree}->[0], $hash{$parameterLevelOne}->{$parameterLevelTwo}->{$parameterLevelThree}->[1], and so on for each item I want to assign.
Only then can I iterate over each one individually, doing this:
foreach (@names) {
print $_;
}
I hope I made this clear. Is this the way Perl is supposed to deal with nested arrays? Can you access each nested list as an array without reassigning EACH and EVERY list element? Thanks.
I currently have a database that's an array inside several hashes (references) all inside a "real" hash. It looks like this:
my %hash = (
'paramterLevelOne' => {
'paramaterLevelTwo' => {
'parameterLevelThree'
=>'Item1', 'Item2', 'Item3', 'Item4'],
What I'm trying to do is print the whole array at the end like this:
@names = $hash{$parameterLevelOne}{$parameterLevelTwo}{$parameterLevelThree};
foreach (@names) {
print @$_;
}
This prints out all the items fine EXCEPT as one single string, not four.
In order to print all four items as separate list elements I have to write all this junk:
@names = ($hash{$parameterLevelOne}->{$parameterLevelTwo}->{$parameterLevelThree}->[0], $hash{$parameterLevelOne}->{$parameterLevelTwo}->{$parameterLevelThree}->[1], and so on for each item I want to assign.
Only then can I iterate over each one individually, doing this:
foreach (@names) {
print $_;
}
I hope I made this clear. Is this the way Perl is supposed to deal with nested arrays? Can you access each nested list as an array without reassigning EACH and EVERY list element? Thanks.