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

Hi Guys, One last question for you Perl Guru's and I'll leave you boys alone...promise :) I'm trying to figure out how to diplay the entries in a database in, say, 4 table fields instead of them being displayed vertically one. So, here's an example of what I'm trying to accomplish:
Entry 1 Entry2 Entry 3 Entry 4 Entry 5 Entry6 Etc. Instead of... Entry 1 Entry 2 Entry 3
Here's what I get to work with...
&parse_form; $database='categories.dat'; if ($input{'database'} eq ''){ $db=$database; }else{ $db=$input{'database'}; } open (ORGDB,"<$database"); @ODB=<ORGDB>; close (ORGDB); shift @ODB; @ODB = sort(@ODB); foreach $rec (@ODB){ chomp($rec); ($Category)=split(/\|/,$rec); print "<tr><td valign=top width=65> <B>Category: </B>$Category<BR></td></tr>\n"; } sub parse_form { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); if (length($buffer) < 5) { $buffer = $ENV{QUERY_STRING}; } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $input{$name} = $value; } }
Thanks Again! Lisa

Replies are listed 'Best First'.
Re (tilly) 1: Database Layout
by tilly (Archbishop) on Jan 31, 2002 at 02:44 UTC
    This is what splice is for.
    my @list = 1..30; while (@list) { print join "\t", splice(@list, 0, 4); print "\n"; }
      This is something I've had on low priority for my own script recently. I did sort of what tilly did but in the messy C-style way with nested for loops. (forgive me please..it was just something to hold over until I got other things done) Problem is that not all entries are the same length, so tabbing doesn't ensure even columns.

      I was wondering whether the @<<<<< formatting placeholders might work for this? I skimmed Chapter7 of Programming Perl, but I confess I haven't studied it carefully yet.

Re: Database Layout
by gav^ (Curate) on Jan 31, 2002 at 02:14 UTC

    Use CGI. Don't reinvent the wheel badly.

    Using something like MLDBM or a database or use something like DBI::RAM to handle a flatfile database better.

    Hope this helps...

    gav^

Re: Database Layout
by screamingeagle (Curate) on Jan 31, 2002 at 02:36 UTC
    to display the fields horizontally (4 per row , in this case) instead of vertically, try something like this :
    $cnt=1; print "<tr>\n"; while (($cnt++ % 5) != 0) { print " <td>test</td>\n"; } print "</tr>";
Re: Database Layout
by Fletch (Bishop) on Jan 31, 2002 at 05:51 UTC

    There's an example in the Perl Cookbook on how to display items in a list a la ls. Check out words from chapter 4.

    Update: Not that that has anything to do with displaying them in the context of an HTML table. Pay no attention to me, I just need (more) sleep.