Using Perl v5.22.1 on Windows 10 x64. I have set
use strict;
use warnings;
This code (based on accepted answer in this question by @Jeef) is part of a while loop parsing a file. It silently exits:
my %hvac_codes; # create hash of unique HVAC codes
#Check the hash to see if we have seen this code before we write it out
if ( $hvac_codes{$hvacstring} eq 1) {
#Do nothing - skip the line
} else {
$hvac_codes{$hvacstring} = 1;
}
If I change it like this
my %hvac_codes; # create hash of unique HVAC codes
#Check the hash to see if we have seen this code before we write it out
if ( defined $hvac_codes{$hvacstring} ) {
#Do nothing - skip the line
} else {
$hvac_codes{$hvacstring} = 1;
print (" add unique code $hvacstring \n");
}
it does not silently exit (also curious as to why silent exit rather than error on undefined reference) but does not work as expected. Every $hvacstring gets added to the %hvac_codes hash, even if they have been added. (as evidenced by print)
I wanted to see how the hash ended up to determine if every code is treated as undefined because the test is wrong, or the assignment into the hash is not working. I tried dumper two ways:
print dumper(%hvac_codes);
and (based on this question's answer)
print dumper(\%hvac_codes);
In both cases the dumper line fails with the Global symbol "%hvac_codes" requires explicit package name
error, even though my %hvac_codes;
is present. For now I have commented it out.