Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How can I display DBM entries in an HTML table? (Revised)

by bladx (Chaplain)
on Dec 29, 2001 at 09:26 UTC ( [id://135072]=perlquestion: print w/replies, xml ) Need Help??

bladx has asked for the wisdom of the Perl Monks concerning the following question:

This is the revised question and code, many thanks go to jepri for all the changes, and the obvious mistake I made (didn't use strict;) anyways, on to the real question, but first a little background. I am creating a simple news database program that saves to a DBM style database, and can add entries into it, using hashes. I am also using HTML::Template for the first real time to seperate the code from the HTML. My question is, how can I have this code snippet display all of the database entries in a table? Also a second question I have about implementing this type of code, is: how can I display all the entries in a form where each of the contents of each hash row is seperated ... like by using split? Thanks for any help! note:I have not added the file lock yet, but that will be added before I actually use this code in a bigger project later. I am sure that there is a lot of the program that could be much more efficient than it is, if so, please let me know, so I can learn and fix it! Thanks again.

Here is the first file, the actual code file (news.pl):

#!/usr/bin/perl -w use strict; use HTML::Template; use CGI qw (:standard); use CGI::Carp qw(fatalsToBrowser); use DB_File; my @fields = ("title","date","author","news"); my $q = new CGI; print $q->header; my $template = HTML::Template->new(filename => 'news.tmpl'); my $filename="database.db"; tie(my %hash, "DB_File", $filename) or die "Can't open $filename: $!"; #add(); Temporarily not used. my @loop_data = (); # initialize an array to hold your loop my @keys = (); my @values = (); 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); } #delete(); Temporarily not used. untie %hash; $template->param( SHOW_ALL_LOOP => \@loop_data); print $template->output; sub add { my $key = time(); my $record = $key; foreach my $field(@fields){ $record .= "\|$field"; } $hash{$key} = "$record"; } sub delete { %hash = (); }


And here is the HTML Template file for the code (news.tmpl):

<html> <head><title>News</title></head> <body bgcolor=#eeeeee> <center> <TMPL_LOOP NAME="SHOW_ALL_LOOP"> <table width=400 border=1 cellspacing=0 cellpadding=0> <tr><td valign=top bgcolor=#dddddd> <b>Key</b>:<TMPL_VAR NAME="KEY"><br> <b>Value</b>:<TMPL_VAR NAME="VALUE"><br><br> </td></tr> </table><br> </TMPL_LOOP> </center> </body> </html>


That should be all you need to check this program. By the way, this is not a homework question, this is a question that I would like to figure out how to do this sort of program correctly, because last summer I created a web framework news type of thing, but it sucked, and I am hoping this will be better overall than my last project. The old project's site page is at www.hashes.f2s.com ... but it is terrible ^_- so don't see it.

Andy Summers

Replies are listed 'Best First'.
map can help you
by larryk (Friar) on Dec 29, 2001 at 19:55 UTC
    An optimisation for you using map:

    Replace the following

    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); }
    with...
    my @loop_data = map { { KEY => $_, VALUE => $hash{$_} } } keys %hash;
    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.

    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>:&nbsp;<TMPL_VAR NAME="KEY"> <b>Value</b>:&nbsp;<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>:&nbsp;<TMPL_VAR NAME="KEY"> </td> <td> <b>Value</b>:&nbsp;<TMPL_VAR NAME="VALUE"> </td>
       larryk                                          
    perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://135072]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-24 18:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found