Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

keys of hash of reference to hash

by melguin (Pilgrim)
on Jan 28, 2003 at 00:50 UTC ( [id://230433]=perlquestion: print w/replies, xml ) Need Help??

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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://230433]
Approved by Ovid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2026-01-17 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (121 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.