获取哈希哈希的密钥

时间:2013-03-19 14:52:50

标签: perl

我正在尝试获取哈希散列的键并始终有错误:

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

如何获取哈希哈希的键?

2 个答案:

答案 0 :(得分:3)

这有点棘手。正确的语法是

keys %{$oop_hash{'wfh'}}

另外,正如您所写,print statement will not quite do what you want。由于Perl解析该行的方式,"\n"不会附加到字符串。你不得不说:

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

答案 1 :(得分:0)

这里是:

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

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