我正在尝试获取哈希的哈希键并且总是有错误:
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
如何获取哈希的哈希键?
我正在尝试获取哈希的哈希键并且总是有错误:
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
如何获取哈希的哈希键?
这有点棘手。正确的语法是
keys %{$oop_hash{'wfh'}}
此外,正如您编写的那样,您的print 语句不会完全按照您的意愿行事。由于"\n"
Perl 解析该行的方式,不会将其附加到字符串中。您必须说以下之一:
print +(keys %{$oop_hash{'wfh'}}),"\n";
print ((keys %{$oop_hash{'wfh'}}),"\n");
这里是:
#!/usr/bin/perl
use strict;
use warnings;
my %oop_hash = ();
$oop_hash{'wfh'}{'ppb'} = "451103";
print join ", ", keys $oop_hash{'wfh'} , "\n"; # "ppb, "