Yannick Warnier <[EMAIL PROTECTED]> writes:
> Hi,
>
> I'm just trying to make a structure with a hash containing some
> references to other (yet unused) hashes.
>
> So what I wrote this:
>
> $intHash1Ref = {};
> $intHash2Ref = {};
>
> %containerHash = { hash1 => $intHash1Ref, hash2 => $intHash2Ref};
>
> Then when I try to have a list of keys to that containerHash:
>
> print keys(%containerHash);
>
> I get some hexadecimal values like:
>
> HASH(0x813f9b0)
>
> How can I manage to do that cleanly?
> I'm searching in "Programming Perl 3th Ed." for that but I don't get
> it... yet.
A couple of problems here:
First, always use 'use strict' and 'use warnings'.
Second, use 'my $intHash1Ref', etc.
Third, the curly braces '{ }' are for hash references (in this
context), not a hash, so:
> %containerHash = { hash1 => $intHash1Ref, hash2 => $intHash2Ref};
should be:
my %containerHash = ( hash1 => $intHash1Ref, hash2 => $intHash2Ref );
The above changes clean things up enough so that we can look at your
actual question. The output of the script now is:
'hash1hash2'
Which are the keys of %containerHash, instead of HASH(0xblahblah).
To address your actual question, what you were printing was the hash
*reference* in $containerHash - the string representation of which
isn't very useful to you. If you just want to look at the hash for
debugging purposes, use Data::Dumper:
use Data::Dumper;
[ your code ...]
print Data::Dumper->Dump([(%containerHash)]);
Which, with the changes I mentioned above, gives you:
$VAR1 = 'hash1';
$VAR2 = {};
$VAR3 = 'hash2';
$VAR4 = {};
Data::Dumper would have expanded $VAR2 and $VAR4 if the hashes
referenced by $intHash1Ref and $intHash2Ref had anything in them.
Data::Dumper isn't really user-friendly enough for actual output
though. For that, you need to access the hashes directly:
#First, loop over the keys of %containerHash:
foreach my $hkey (keys %containerHash) { # 'hash1', then 'hash2'
#Now, loop over the keys in each of the hashes stored in %containerHash:
foreach my $intkey (keys %{$containerHash{$hkey}}) {
# $containerHash{$hkey} gives us the hash reference which is the value
# of $containerHash for the given key. Also known as $intHash1Ref and
# $intHash2Ref. We dereference that hashref with '%{ }', and loop over
# the keys.
my $value = $containerHash{$hkey}->{$intkey};
# This time we're dereferencing the hashrefs in $containerHash with
# '->{$intkey}' and getting the value stored in $intHash1Ref (or 2)
# for that key.
print "Hash '$hkey': key = '$intkey', value = '$value'";
}
}
Oh, and we usually don't capitalize variables in perl. See: perldoc
perlstyle for recommendations.
Example code:
#!/usr/bin/perl -l
use strict;
use warnings;
use Data::Dumper;
my $intHash1Ref = { one => 1, two => 2, three => 3 };
my $intHash2Ref = { ten => 10, eleven => 11, twelve => 12 };
my %containerHash = ( hash1 => $intHash1Ref, hash2 => $intHash2Ref );
print Data::Dumper->Dump([(%containerHash)]);
foreach my $hkey (keys %containerHash) { # 'hash1', then 'hash2'
foreach my $intkey (keys %{$containerHash{$hkey}}) { # the keys of $intHash1Ref and
$intHash2Ref
my $value = $containerHash{$hkey}->{$intkey};
print "$hkey: key = '$intkey', value = '$value'";
}
}
-RN
--
Robin Norwood
Red Hat, Inc.
"The Sage does nothing, yet nothing remains undone."
-Lao Tzu, Te Tao Ching
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]