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($_)) ."&nbsp;} (@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); }

In reply to Re: Hash of Arrays by Lhamo Latso
in thread Hash of Arrays by Lhamo Latso

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.