I have a Perl script that loads each line of a text file into a hash.
&load_map($word_file,"words");
sub load_map {
my ($file_path,$assoc) = @_;
open FILE, $file_path or die "Cannot open file $file_path";
while ($line = <FILE>) {
chomp $line;
$$assoc{$line}=1;
}
close FILE;
}
Later on in the script, I look in the hash to see if a word is there before I do something:
if($words{$wordKey}) {
do something...
}
This works fine when I am running the script in my local environment. When I am running this script in a docker container, $words{$wordKey}
has no value or isn't found so my code that I need to run never executes.
print $words{$wordKey}
is empty in a docker container. It is 1 in my local environment.
The strange thing is, I know the hash data does exist in the docker container because if I loop through the entire hash and look at the keys 1 by 1, I can see each key and eventually get to the key I'm looking for, but this defeats the entire purpose of a hash. I shouldn't have to loop through the entire hash from beginning until I find my key.
while( my( $key, $value ) = each %words ){
if($key == $wordKey) {
Do something
}
}
Has anyone ever come across this problem and understand what I may or may not be doing that is causing this issue? It's driving me crazy.
===============================================================
New code and output
CASE 1:
&preload_words($word_file);
sub preload_words {
my %localhash;
my ($file) = @_;
open my $fh, '<', $file or die "Can't open $file: $!";
my $avalue;
while (my $line = <$fh>) {
chomp $line;
$localhash{$line}=1;
print "word: $line, local value: $localhash{$line} \n";
$avalue = $line;
}
close $fh;
print "a word: $avalue, a value: $localhash{$avalue} \n";
my $bvalue = $localhash{'written'};
print "b word: written, b value: $bvalue \n";
}
outputs:
...
word: worn, local value: 1
word: win, local value: 1
word: won, local value: 1
word: write, local value: 1
word: wrote, local value: 1
word: written, local value: 1 <-- last print from loop
a word: written, a value: 1 <-- avalue last dynamically set from loop
b word: written, b value: 1 <-- hardcoded last word of loop print
CASE 2:
&preload_words($word_file);
sub preload_words {
my %localhash;
my ($file) = @_;
open my $fh, '<', $file or die "Can't open $file: $!";
my $avalue;
while (my $line = <$fh>) {
chomp $line;
$localhash{$line}=1;
print "word: $line, local value: $localhash{$line} \n";
$avalue = $line;
}
close $fh;
print "a word: $avalue, a value: $localhash{$avalue} \n";
my $bvalue = $localhash{'wrote'};
print "b word: wrote, b value: $bvalue \n";
}
...
word: worn, local value: 1
word: win, local value: 1
word: won, local value: 1
word: write, local value: 1
word: wrote, local value: 1 <-- prints value in loop but nothing outside of loop scope.
word: written, local value: 1 <-- last print from loop
a word: written, a value: 1 <-- avalue last dynamically set from loop
b word: wrote, b value: <-- hardcoded 2nd to last word of loop print returns no value
It appears to be some sort of scoping issue in the container even within the same function. Once I'm out of the while loop, I can only dereference the very last index directly. Thoughts?