这个问题似乎是范围之一。带有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;
}
}
}
答案 0 :(得分:3)
您必须在标量exists
$hash{key}
if (exists $pn_codes{$pn}) {
但是,您实际上是在创建一个%seen
样式哈希,可以简化为:
while (<FH>) {
if (/.*PN=(\d*)/) {
my $pn = $1;
if (! $pn_codes{$pn}++) {
print($pn, "exists");
}
}
}
答案 1 :(得分:1)
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