Hi guys,
I encountered this dereference problem on hash-of-lists tables. Although I have
managed to solve the problem thru trial and error, I need someone to explain to me
what was wrong exactly. I tried searching the web for some info but couldn't find any.
The problem started when I tried to assign one of the values in a hash-of-lists table
to another hash-of-lists table.... something like this :
foreach my $time ( keys %hash1) {
if ($hash2{$time}[0] > 0) {
if ($hash1{$time}[0] >= $hash2{$time}[0]) {
push @{ $final_hash{$time}[0] }, $hash1{$time}[0];
}
else {
push @{ $final_hash{$time}[0] }, $hash2{$time}[0];
}
}
if ($hash2{$time}[1] > 0) {
if ($hash1{$time}[1] > $hash2{$time}[1]) {
push @{ $final_hash{$time}[1] }, $hash1{$time}[1];
}
else {
push @{ $final_hash{$time}[1] }, $hash2{$time}[1];
}
}
}
The above code has no errors. But when I tried to display the content of the hash
table "final_hash" with the following code :
foreach my $time ( keys %final_hash) {
print "$time $final_hash{$time}[0]
$final_hash{$time}[1]\n";
}
I get the following reply :
1006788300 ARRAY(0x80ecf44) ARRAY(0x80ecf68)
1006788600 ARRAY(0x80ed01c) ARRAY(0x80ed040)
1006787700 ARRAY(0x80ecfbc) ARRAY(0x80ecfe0)
1006788000 ARRAY(0x80ed094) ARRAY(0x80ed0b8)
1006788900 ARRAY(0x80ed0f4) ARRAY(0x80ee2bc)
That showed it was a case of dereference. After multiple tries and changes made to the
push and print statements, I found out that my problem is solved by changing the print
statement to the following :
foreach my $time ( keys %final_hash) {
print "$time @{$final_hash{$time}[0]}
@{$final_hash{$time}[1]}\n";
}
Can anyone briefly explain to me on that??
Also, I believe there are other ways to solve the problem by changing the PUSH or
PRINT statements. Can anyone point them out to me??
Thanks a lot. :)