Reputation: 770
This problem seems to be one of scope. The line with the exists function on it throws an error saying that it's not receiving a hash as an argument. How can I make so the value being passed to the exists function is my hash?
#!/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;
}
}
}
Upvotes: 1
Views: 3532
Reputation: 35198
You must specify exists
on a scalar $hash{key}
if (exists $pn_codes{$pn}) {
However, you're essentially creating a %seen
style hash which can be simplified to just:
while (<FH>) {
if (/.*PN=(\d*)/) {
my $pn = $1;
if (! $pn_codes{$pn}++) {
print($pn, "exists");
}
}
}
Upvotes: 3
Reputation: 50637
perl diagnostics
can be useful,
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
Upvotes: 1