Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Splitting an array into multiple HTML table rows

by mikeirw (Pilgrim)
on Sep 25, 2002 at 16:47 UTC ( #200664=perlquestion: print w/ replies, xml ) Need Help??
mikeirw has asked for the wisdom of the Perl Monks concerning the following question:

I'm having trouble coming up with a sane way of splitting the contents of an array into multiple HTML table rows. I've got a couple of constants defined, NUM_CELLS => 12 and NUM_COL => 3. The array, of course, contains twelve elements. The problem I'm having is figuring out how to get those twelve elements split into four table rows. The relevant code that I have so far is something like this:
use CGI qw/ *table *Tr /; use constant NUM_CELLS => 12; use constant NUM_COL => 3; sub fill_table { my @array = read_db; my @td; push @td, $cgi->start_Tr(); foreach ( @array[ 0 .. NUM_COL - 1 ] ) { push @td, $cgi->td($_); } push @td, $cgi->end_Tr; return @td; }
So, I'm getting the first three elements into the first row, but after that I'm stuck with coming up with a way to get the rest of the table filled out. I'd appreciate any ideas from fellow monks,

Comment on Splitting an array into multiple HTML table rows
Download Code
•Re: Splitting an array into multiple HTML table rows
by merlyn (Sage) on Sep 25, 2002 at 17:05 UTC
      My apologies, Mr. Schwartz. I actually did search the site for an answer, but I guess I wasn't looking in the right places. Anyway, thanks for the link.
Re: Splitting an array into multiple HTML table rows
by BrowserUk (Pope) on Sep 25, 2002 at 17:07 UTC

    Take a look at the replies to simple column/row counter this recent question on the same subject.



    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
(jeffa) Re: Splitting an array into multiple HTML table rows
by jeffa (Chancellor) on Sep 25, 2002 at 17:08 UTC
    How about something like this:
    use strict; use CGI qw(:standard); my @array = (0..11); my $max = 4; my ($i,$j) = (0,0); my $tab; for (@array) { push @{$tab->[$j]},$_; $j++ unless ++$i % $max; } print table(map{ Tr(td $_)} @$tab);

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Splitting an array into multiple HTML table rows
by mirod (Canon) on Sep 25, 2002 at 17:10 UTC

    It's funny, I was just reading the description of XML-Filter-TableWrapper just before I read your question. Wouldn't it solve your problem?

Re: Splitting an array into multiple HTML table rows - short version
by fglock (Vicar) on Sep 25, 2002 at 17:56 UTC
    print "<tr><td>", join ("</td><td>", splice (@array, 0, NUM_COL) ), "</td></tr>\n" while @array;

    note:  splice eats up the array.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2014-12-19 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (74 votes), past polls