这个问题似乎是范围问题之一。exists函数所在的行抛出一个错误,说它没有接收到哈希值作为参数。我怎样才能使传递给exists函数的值是我的哈希值?
#!/usr/bin/perl
use warnings;
use strict;
open FH, 'test_out' or die $!;
my %pn_codes = ();
while(<FH>) {
if(/.*PN=(d*)/) {
my $pn = $1;
if(exists %pn_codes{$pn}) {
print($pn, "exists");
} else {
%pn_codes{$pn} = 1;
}
}
}
必须在标量$hash{key}
上指定exists
if (exists $pn_codes{$pn}) {
然而,你实际上是在创建一个%seen
风格的散列,它可以简化为:
while (<FH>) {
if (/.*PN=(d*)/) {
my $pn = $1;
if (! $pn_codes{$pn}++) {
print($pn, "exists");
}
}
}
perl diagnostics
可以是有用的,
perl -Mdiagnostics -c script.pl
exists argument is not a HASH or ARRAY element or a subroutine at c line 13 (#1)
(F) The argument to exists() must be a hash or array element or a
subroutine with an ampersand, such as:
$foo{$bar}
$ref->{"susie"}[12]
&do_something
Uncaught exception from user code:
exists argument is not a HASH or ARRAY element or a subroutine at c line 13.
at c line 13