in reply to How can I display DBM entries in an HTML table? (Revised)
Replace the following
with...my @loop_data = (); my @keys = (); my @values = (); # incidentally, the "my %hash" on the next line should be just "%hash" while ((my $key, my $value) = each my %hash) { push(@keys, $key); push(@values, $value); } while (@keys and @values) { my %row_data; $row_data{KEY} = shift @keys; $row_data{VALUE} = shift @values; push(@loop_data, \%row_data); }
map is a way to build lists from lists. in this case you can use it to iterate through the keys of %hash and create a hashref for each key/value pair to be stored in @loop_data.my @loop_data = map { { KEY => $_, VALUE => $hash{$_} } } keys %hash;
Don't be confused by the { { ... } } syntax though - map likes a block (i.e. map { ... } list) or a single expression (i.e. map expression, list - notice the comma) but since you want hashrefs ({})we have to use a block or it will get confused when you put a comma after. Maybe I just confused you even more - RTFM and you should see what I mean.
Hope this helps.
Update
Regarding your actual question I think if you put the loop inside the <table></table> around the rows then it'll all be in the same table - is that what you were asking? Sorry, I'm feeling a bit thick today!
<table width=400 border=1 cellspacing=0 cellpadding=0> <TMPL_UNLESS SHOW_ALL_LOOP> <tr> <td> Nothing to see here folks, move along! </td> </tr> </TMPL_UNLESS> <TMPL_LOOP SHOW_ALL_LOOP> <tr> <td valign=top bgcolor=#dddddd> <b>Key</b>: <TMPL_VAR NAME="KEY"> <b>Value</b>: <TMPL_VAR NAME="VALUE"> </td> </tr> </TMPL_LOOP> </table><br>
Update 2
Check out DB_File::Lock for obvious reasons.
Update 3
For your second question you could just put the template vars in separate table cells:
<td valign=top bgcolor=#dddddd> <b>Key</b>: <TMPL_VAR NAME="KEY"> </td> <td> <b>Value</b>: <TMPL_VAR NAME="VALUE"> </td>
larryk perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
|
---|