[Za-pm] passing HoH refs to a sub

Spike spikeh at mweb.co.za
Tue Jul 8 05:58:18 CDT 2003


I have a hash of hashes and want to pass a reference to that HoH to a sub 
routine (which we call 'print_it' for this example).

So I go:-

<cut>
print_it(\%HoH);
<cut>

Now my problem comes when I want to de-reference the HoH values:-

<cut>

sub print_it

{
my $hash_ref = shift;
    foreach $key (keys %$hash_ref)
    {
       print "$key: ";
# up to here all is well!

# but I can't get the syntax to access the 'child' hash values
# This does not work:-

       foreach $code ( keys %{ %$hash_ref{$key} } )
       {
          #  print "$code = %$hash_ref{$key}{$code} ";
       }
# the problem is in the 'keys %{ %$hash_ref{$key}' bit.

<cut>

Using the hash directly the following would be valid:-

foreach $key (keys %HoH)
{
    print "$key: ";
    foreach $code ( keys %{ $HoH{$key} } )
    {
       print "$code = $HoH{$key}{$code} ";
    }
    print "\n";
}

Any ideas on the correct syntax??




Spike Hodge

UNIX Programmer
M-Web Technology
021 596 8496
082 901 5265

Click here and make M-Web your homepage
http://homepage.mweb.co.za 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/za-pm/attachments/20030708/f5e95886/attachment.htm


More information about the Za-pm mailing list