#!/usr/bin/perl -w
use strict;
use Data::Dump qw(pp);
use Data::Dumper;
my %totalhash;
# the value of $totalhash{hello1}{world} is a reference
# to an array, a HoHoA
$totalhash{hello1}{world} = ["hello world", "hello2", "hello3"];
# I would not do this, ie make a single value instead of a ref to
# array... make all of the values ref to array even if there
# is only one value for some ...makes the structure more "consistent".
# I just show it as a possibility.
#
$totalhash{diffHello}{world} = "different hello";
print "@{$totalhash{hello1}{world}}\n"; # ** need curly braces
# not ()!!
#prints: hello world hello2 hello3
# I personally would prefer the arrow notation which makes it
# more clear to me that this a reference to array, but you
# can leave that out if you wish.. Here this is a minor point.
print $totalhash{hello1}{world}->[1],"\n";
#prints: hello2
print pp \%totalhash;
print "\n";
# the pp option to Data::Dump is very good, but
# since Data::Dump is not a core module and requires installation,
# this can be problematic.
# Data::Dumper is available on all Perl systems and is "core"
print Dumper \%totalhash;
__END__
#### All print statements: ###
hello world hello2 hello3
hello2
{
diffHello => { world => "different hello" },
hello1 => { world => ["hello world", "hello2", "hello3"] },
}
$VAR1 = {
'diffHello' => {
'world' => 'different hello'
},
'hello1' => {
'world' => [
'hello world',
'hello2',
'hello3'
]
}
};
|