I want to look up multiple values from one key. I create two hashes loaded from a separate file. The test code that follows shows - I think - that two hashes are loaded because I can print them from a foreach structure - but I cannot query the hashes directly - Perl prints "Use of uninitialized value in concatenation (.) or string at testhash.pl line 22." , and lines 25 and 28.
I believe that I could use a single hash where keys pointed to array references. But I won't try that before I can make this apparently simple case work. Thanks for any suggestions.
CODE
# external file "test1"
# file values for hash
key1 value1 A value1 B
key2 value2 A value2 B
key3 value3 A value3 B
# end file test1
# <<get data from file>>
$parameter_file = "test1";
open (INFILE, $parameter_file) || die "Couldn't open $parameter_file";
LINE: while (<INFILE>)
{
next LINE if /^#/;
chomp;
@test = split/\t/;
$test_hashA {$test[0]} = $test[1];
$test_hashB {$test[0]} = $test[2];
}
close INFILE;
# <<show data is loaded>>
print "\n\nforeach from hash\n";
print "key\tkey\tvalue\n";
foreach $key (sort keys %test_hashA) {
print "$key\t$test_hashA{$key}\t$test_hashB{$key}\n";
}
# <<try to access hashes>>
# retrieve value for a known key
print "lookup values\n";
print "test1:\t$testhash1{key1}\n";
# does it work for a key as scalar?
$nn = "key1";
print "$nn\ttest_scalar:\t$test1{$nn}\n";
# problem with print? assign value to string
$test_print = $test1{$nn};
print "print-test:\t$test_print\n";
print "existentce test\n";
if (exists $test1{key1}) {print "found\n";} else {print "not found\n"};