Having some trouble adding some keys to a hash, i.e. modifying in a subroutine. Here’s my subroutine call:
getMissingItems($filename, \%myItems); #myItems is already defined above this
and the subroutine itself:
sub getMissingItems {
my $filename = shift;
my $itemHash = shift;
#... some stuff
foreach $item (@someItems) {
if (not exists $itemHash{$item}) {
%$itemHash{$item} = 0;
}
}
}
I get the error “Global symbol %itemHash requires explicit package name”
How should I be doing this properly? Thanks.
EDIT – thanks everyone, over this first hurdle. I’m now getting “Can’t use string (“0”) as a HASH ref while “strict refs” in use.” I just want to set the missing key entry to zero
There is no
%itemHashin scope of your sub, yet you try to use a variable named that.You mean to access the hash referenced by
$itemHash, soshould be
By the way, you could simplify that to
(That checks if the element is
defined, not if itexists, but that’s probably the same thing in this case.)