Thanks for input, and sorry for the delay. I needed some sleep. Your comments have helped me tremendously. Besides not respecting the difference between scalar and list context, my hash was being tied to a DB_File dbm. After commenting the tie operation, the results are in line with everyone else.
But now back to DB_File. Do I need to use a different module for complex data structures? Like maybe MLDBM? My original thought was that DB_File could handle it!???
Here is the code that works for me, but without any hash tie:
## increment the sequencer.
$HoA{sequence} = 0 unless exists $HoA{sequence};
my $i = ++$HoA{sequence};
print ("HoA sequence: $HoA{sequence} \n");
my $date = strftime('%d-%b-%Y %R',localtime);
$HoA{$i} = [ ($date), map {escape(param($_))} (@FIELDS) ];
print ("HoA newvalue $i: $HoA{$i} \n" );
print ("HoA newvalue $i: $HoA{$i}[0] \n" );
print ("HoA newvalue $i: $HoA{$i}[1] \n" );
print ("HoA newvalue $i: $HoA{$i}[2] \n" );
print ("HoA newvalue $i: @{ $HoA{$i} } \n" );
print ("HoA newvalue $i: " . @{ $HoA{$i} } ."\n" );
$HoA{test} = [ "aaa", "bbb", "ccc" ];
print ("HoA test $i: " . $HoA{test} ."\n" );
print ("HoA test $i: " . $HoA{test}[0] ."\n" );
print ("HoA test $i: " . $HoA{test}[1] ."\n" );
print ("HoA test $i: " . $HoA{test}[2] ."\n" );
print ("HoA test $i: @{ $HoA{test} } \n" ); # returns list
print ("HoA test $i: " . @{ $HoA{test} } ."\n" ); # returns count
My plans for the $HoA{sequence} was to use it for saving the last sequence number. It would be a way to know where to insert the next node. Alla Oracle sequence. It seems silly to use a number as a hash key, but I want to tie the hash to a dbm. I could use an Array of Arrays, but is that tie-able?
Update
After more reading, I am now flattening the array before putting it in a tied hash. I used join and split for this. Here is more code; this works for me:
sub write_guestbook {
unless (TieLock($GUESTBOOKFILE, 1)) {
print strong('Sorry, an error occurred: unable to open ' . $GU
+ESTBOOKFILE),p();
return;
}
## increment the sequencer.
$HoA{sequence} = 0 unless exists $HoA{sequence};
my $i = ++$HoA{sequence};
my $date = strftime('%d-%b-%Y %R',localtime);
$HoA{$i} = join(">", ($date), map {escape(param($_)) ." } (@F
+IELDS) );
unTieLock($GUESTBOOKFILE);
return( "Thank you <b>". param('First_Name')
."</b>, for signing the guestbook.");
}
sub view_guestbook {
my $guestbook;
my @rows;
unless (TieLock($GUESTBOOKFILE, 0)) {
print strong('Sorry, an error occurred: unable to open ' . $GU
+ESTBOOKFILE),p();
return;
}
for my $addr (sort keys %HoA) {
next if $addr eq "sequence";
my @data = map {unescape($_)} split( ">", $HoA{$addr} );
unshift @rows, td(\@data);
}
unshift @rows, th(['Entry Date',@FIELDS]);
$guestbook .= p(
table({-border=>'1', -hspace=>'5',
-cellspacing=>'1', -cellpadding=>'4'},
TR(\@rows)));
unTieLock($GUESTBOOKFILE);
return ($guestbook);
}
|