Vitali Pom
Vitali Pom

Reputation: 602

Get keys of hash of hashes

I'm trying to get the keys of hash of hashes and always have errors:

use strict;

my %oop_hash = ();
$oop_hash{'wfh'}{'ppb'} = "451103";
print (keys $oop_hash{'wfh'})."\n"; #1st try
print (keys %oop_hash{'wfh'})."\n"; #2nd try

How can I get the keys of hash of hashes?

Upvotes: 0

Views: 462

Answers (2)

mob
mob

Reputation: 118595

It's a little tricky. The correct syntax is

keys %{$oop_hash{'wfh'}}

Also, as you've written it, your print statement will not quite do what you want. The "\n" will not get appended to the string because of the way Perl parses that line. You'll have to say one of:

print +(keys %{$oop_hash{'wfh'}}),"\n"; 
print ((keys %{$oop_hash{'wfh'}}),"\n");

Upvotes: 3

varnie
varnie

Reputation: 2595

here it is:

#!/usr/bin/perl
use strict;
use warnings;

my %oop_hash = ();
$oop_hash{'wfh'}{'ppb'} = "451103";
print join ", ", keys $oop_hash{'wfh'} , "\n"; # "ppb, "

Upvotes: 0

Related Questions