Reputation: 1234
I am calling a perl subroutine
&ProcessInput($line, \%txcountershash, \%txhash);
I have this definition:
sub ProcessInput() {
my $word;
my $counterkey;
my $line = $_[0];
my $countershash = $_[1];
my $outputhash = $_[2];
# remove all the blanks from the line
$line =~ s/\s+//g;
my ($port,$counter,$value,$datetime) = split('\|',$line,4);
my $counterdesc = $countershash{$counter};
the line that causes the problem is the last line. It says that global symbol %countershash requires explicit package name. I'm not sure why it's giving me the error. There aren't any problems otherwise, if I comment that line out the script runs. The hash is set up as an error code for a key and a description that is the value. I'm trying to get the value at a particular key in $countershash so I can add the error description to the output hash.
Upvotes: 1
Views: 1590
Reputation: 69224
The code $counterhash{$counter}
means "look up the key $counter
in the hash %counterhash
". You don't have a hash called %counterhash
, you have a hash reference in a scalar variable called $counterhash
. %counterhash
and $counterhash
are two completely different variables.
In order to look up an element in a hash reference, you need to use different syntax.
my $counterdesc = $countershash->{$counter};
Please don't use the $$countershash{$counter}
syntax (previously) used in another answer. That will only confuse whoever needs to maintain your code in six months time (which might well be you).
Upvotes: 0
Reputation: 5927
The problem is dereferencing. you should dereference the hash inside the subroutine
my $counterdesc = $countershash->{$counter};
->
this is called arrow operator which is used to deference the array and hashes.
Upvotes: 1