Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options


by Lloyd (Initiate)
on May 09, 2000 at 23:42 UTC ( #10801=perlquestion: print w/replies, xml ) Need Help??
Lloyd has asked for the wisdom of the Perl Monks concerning the following question:

I am a newbie teaching myself, please be patient!

I have a flat file DB that I am accessing through the query string eg. 'offers.cgi?Category&Subcategory' or in the case of getting a list of the subcategories within a category: offers.cgi?Category - This is working fine. I have a hash with the subcats in and the number of offers in each subcat (the flat file is pipe delimited and the fields are cat|subcat|company|add1|add2 etc.)

I am outputting the list of subcats with the number of offers in brackets.
The problem I am having is really a perl/HTML formatting problem I think. I want to be able to display the subcats 3 per table row as in:

<TR> <TD>1st subcat</TD> <TD>2nd subcat</TD> <TD>3rd subcat</TD> </TR> <TR> <TD>4th subcat</TD> <TD>5th subcat</TD> <TD>6th subcat</TD> </TR>
etc. can any1 help me on this??
I can supply more code, so you know what I am doing with the rest of the script.
Thanks in advance.

Replies are listed 'Best First'.
Re: Output
by btrott (Parson) on May 10, 2000 at 00:16 UTC
    So it seems like you're looking for a way to split up a list of sub-categories into bunches of 3, then? Is that it?

    Try this:

    my %subcat = (Foo => 3, Bar => 2, Baz => 10, Quux => 1); my $index = 0; print "<TR>\n"; for my $sc ( keys %subcat ) { print "<TD>", $sc, " (", $subcat{$sc}, ")</TD>\n"; print "</TR>\n<TR>\n" if ++$index % 3 == 0; } print "</TR>\n";
    Does that do what you want?
      you may also want to fill out the last row with empty cells by adding the following code between the for loop and final print call:
      print "<TD>&nbsp;</TD>\n" while (index++ % 3);
      You're a life saver!!

      Thanks a million.
      I might have some more questions, I am a newbie teaching myself so I am running into a lot of problems.

      Thanks again.
Re: Output
by chromatic (Archbishop) on May 10, 2000 at 00:20 UTC
    What does your data structure look like, when it's read into your program? If you have categories in an array, it's not too complicated:
    while (@lines) { $output .= "<TR>"; for ( 1 .. 3 ) { $output .= "<TD>" . (shift @lines) . "</TD>"; } $output .= "</TR>"; }
    You might use the following to make sure you have an appropriate number of things in @lines:
    my $fill = scalar @lines % 3; while ($fill-- > 0) { push @lines, " "; }
RE: Output
by Russ (Deacon) on May 10, 2000 at 04:18 UTC
    See my reply to the other post.
Re: Output
by httptech (Chaplain) on May 10, 2000 at 00:07 UTC
    You may want to look at using HTML::ElementTable for this. It will allow you to set up the entire table up ahead of time, then push the content into the table, change attributes of rows, cells, etc, then display the table.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://10801]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2018-06-25 02:56 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.