melguin has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to get all of the keys in a referenced hash of a hash. Here's my code:
foreach my $pid (keys %$matches) { foreach my $key (keys $$matches{$pid}) { print "$pid:$key:$$matches{$pid}{$key}\n"; } }
The error code tells me that it's not a hash. What is the correct syntax?

thanks as always,

melguin

Replies are listed 'Best First'.
Re: keys of hash of reference to hash
by sauoq (Abbot) on Jan 28, 2003 at 00:55 UTC

    I think you want your second foreach to look more like:

    foreach my $key (keys %{$matches->{$pid}}) { ... }

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: keys of hash of reference to hash
by elusion (Curate) on Jan 28, 2003 at 00:59 UTC
    Short answer:
    foreach my $key (keys %{$matches->{$pid}}) {

    Long answer:
    You can think of $matches->{$pid} as the name of your hash. So now that you have the name, you put the hash symbol (%) in front and wrap it in curly braces to disambiguate, and you're all set.

    elusion : http://matt.diephouse.com

      Shout praises to the all-mighty {}!!!
Re: keys of hash of reference to hash
by Ovid (Cardinal) on Jan 28, 2003 at 01:18 UTC
    #!/usr/bin/perl -w use strict; my $matches = { foo => { 1 => 'one', 2 => 'two' }, bar => { this => 'that' } }; while ( my ($pid,$pid_values) = each %$matches ) { while ( my ($key, $value) = each %$pid_values ) { print "$pid:$key:$value\n"; } }

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)