http://qs321.pair.com?node_id=235616

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

Hello Monks,

I am rewriting a web form, because the old form hard coded *everything*, and I am trying to make it a bit more dynamic and flexible. The form allows folks to update stuff stored in an AoA called @champarray in the code below. I have it dynamically generating a table with all the items, but I want to make it break the rows up, so that there are not more than five tds per tr (so that the page does not get absurdly wide), somehow nothing reasonable is entering my mind when I think about this, and I think I must be missing an obvious solution. I created a $rownum var that determined how many rows to subdivide in, but then did not really know what to do with it. The code below is a little funny looking since I am still in the middle of thinking about this. In the context of this code, what is the best way to iterate by fives to fill the rows? Any other constructive criticism would also be appreciated.

The relevant chunk:
my $productcount = 0;#index of which product we are using print $q->table({-border=>1}); foreach (@champarray){# go through each item. my @rows; my @tmp_row_items = $q->p($champarray[$productcount][0]);#item 0 i +n array is the name of our item my $itemnum = $#{$champarray[$productcount]}; my $rownum = ceil($itemnum / 5);#ceil is POSIX round up function for my $i (1 .. $itemnum){ push (@tmp_row_items, $q->td($q->textfield( -name => "$champar +ray[$productcount][0]$i", -default => "$champarray[$productcount][$i] +", -size => 25))); } push (@rows, $q->td(@tmp_row_items)); print $q->Tr(\@rows); $productcount++; }

Replies are listed 'Best First'.
Re: How to iterate by fives?
by boo_radley (Parson) on Feb 15, 2003 at 22:53 UTC
    use the modulus operator -- % isn't just for hashes.
    for (1..100) { print "$_ "; print "\n" if ($_ % 5 ==0); # add a break (or other string) every +5 elements }
    There are other ways to do it, but that's the most perl, I think.
    Other ways?
    @foo = map {$_*5} (1..10); # generate a list by fives. print join " ", @foo,"\n\n"; # see the fives. print "<table>"; # replace with calls to CGI.pm as appropriate. for my $ctr (0..$#foo) { print " <tr>\n "; print " <td>$_</td>" for (($ctr*5)+1 .. $foo[$ctr]); print "\n </tr>\n"; } print "</table>\n\n";
      Thanks! I knew I was missing the obvious here. A modulus did the trick:
      for my $i (1 .. $itemnum){ push (@tmp_row_items, $q->td($q->textfield( -name => "$champar +ray[$productcount][0]$i", -default => "$champarray[$productcount][$i] +", -size => 25))); if ($i % 5 == 0) { push (@rows, $q->td(@tmp_row_items)); @tmp_row_items = (); } }
Re: How to iterate by fives?
by BrowserUk (Patriarch) on Feb 15, 2003 at 23:18 UTC

    This is one of those exceptions that proves the rule. This is one of the few cases when I think that the C-style for(;;) has much going for it relative to the (many) other possible approaches.

    #! perl -slw use strict; use CGI qw[:standard *table]; my @data = 1 .. 103; print start_table( {-border=>1} ); for( my $i = 0; $i < $#data; $i += 5 ) { print Tr( td( [ map{ defined $_ ? textfield( { -name=>$_, -default=>$_ } ) : '&nbsp;' } @data[$i .. $i+4] ] ) ); } print end_table;

    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: How to iterate by fives?
by Coruscate (Sexton) on Feb 15, 2003 at 23:31 UTC

    I think the solution for this is going to be pretty difficult and exteremely ugly html. Let's say the first entry in @champarray has a total of 5 items. Perfect: it'll all fit in one row with 5 columns. Now, say the second entry has 7 entries. You'll fill one row and then 2 columns of the next row. That'll leave you with 3 empty columns in that second row, so in order to create valid html, you'll have to output a print td() x 3;. So yes, this would be possible, but I guarantee it'll look ugly in code, in html and visual table output. Blank cells all over just wouldn't look very appealing.

    So is there no help for this? Of course there is! What I wholeheartedly propose is rather than filling columns like this, use rows. Here's some code that creates very appealing output. Code is tested, because I wasn't too sure of myself :) I really hope you consider using something based on the below code. It looks nicer, it's easy html and it's better! I changed the variable names, just because I didn't like the ones you were using :)

    #!/usr/bin/perl -Tw use strict; use CGI qw/:standard *table/; my @products = ( ['Dog', 'Rover', 'Hairy', 'Smelly'], ['Cat', 'Snowball', 'Sharp', 'Smelly'], ['Mouse', 'Mousey', 'Tiny', 'Cheese'] ); print header(), start_html('Product Info'), start_table( {cellpadding=>5,border=>2,align=>'center'} ); for my $product (@products) { print Tr( th( {align=>'left'}, 'Product ID/Name:' ), td( $product->[0] ) ); my $i; for my $entry (@{$product}) { next if ++$i == @{$product}; print Tr( td( '&nbsp;&nbsp;&nbsp;&nbsp;Entry #', $i, ':' ), td( textfield( {name=>$product->[0].$i,value=>$product->[$i],size=>25} ) ) ); } print Tr( td( {colspan=>2}, p() ) ); } print end_table(), end_html();


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, reply to this node or /msg me to tell me what is wrong with the post, so that I may update the node to the best of my ability. If you do not inform me as to why the post deserved a downvote, your vote does not have any significance and will be disregarded.

      Thanks for the ideas. This is pretty much in line with what I had originally made, since it certainly is simpler to generate, but the original I am replacing has items in colums, and I need it to be as transparent an update as I can make it. The form tracks email addresses of folks associated with certain products. There are currently about fourty products with five to ten email addresses associated with a number of them, so it wound up appearing really clumsy to me. Since the number of products will only increase, and one of the reasons I was rewriting it was because there was a hard limit of ten, and we had cases where up to twenty email addresses would be needed, with the possibility of this increasing even more over time, which starts to get really awkward in one giant column.

      I am not really an html guy, I was not aware that a different number of columns/row in a table was invalid. The stuff I generated renders correctly in all the browsers I need to test, but I will have to consider adding blank cells, since I want it to be correct. Since I have no borders on the table anyway, a blank cell and no cell have basically the same appearance. Again thanks.
        In your HTML, you may need to put &nbsp; in the empty <td> elements in order for the blank space to be 'filled in' by the browser. YMMV.

        This observation is based on purely anecdotal evidence; I hope it turns out to be more than just a brain-fart.

Re: How to iterate by fives?
by caedes (Pilgrim) on Feb 15, 2003 at 22:37 UTC
    I find that it is awfully cumbersome to write forms and tables with CGI.pm. I suggest using HTML::Template. It lets you write your perl in perl and your html in html.

    -caedes

      Yes. HTML::Template++. I've used this module a number of times, and it makes dealing with re-formatting the HTML so much nicer. The other advantage is that you can hand the template file to your web-designer, (who if you are unlucky, does not speak perl) and they can deal with it in their WYSIWYG editor, if it will play nice with non-standard tags.

      The other nice thing is that you can put loops in your templates so you don't have to repeatedly loop to generate repetitive HTML.

      -Il Cylic

      Section 66: Cruel, unjust, and dedicated to death.
      Thanks, since you mentioned it I have been looking at HTML::Template, and this looks like it is definitely the way to go down the line. For the short term I just needed to get this thing out the door, but now that I have doe a bit of experimenting with it, I definitely want to migrate all my CGIs over to HTML::Template, it looks like it should make life a lot better. Again, thanks.
Re: How to iterate by fives?
by zengargoyle (Deacon) on Feb 17, 2003 at 16:14 UTC

    i didn't see anything like this for chunking up the list. this way makes me warm and fuzzy...

    @things = 'a'..'z'; print "#things", $/; print "@things", $/; $by = 5; print "#by", $/; print "$by", $/; @extra = ('.') x $by - 1; print "#extra", $/; print "@extra", $/; @padded_things = (@things, @extra); print "#padded_things", $/; print "@padded_things", $/; print "#chunked", $/; while ( (@chunk = splice @padded_things, 0, $by) == $by ) { print "@chunk", $/; }
    $ perl test.pl 
    #things
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    #by
    5
    #extra
    . . . . 
    #padded_things
    a b c d e f g h i j k l m n o p q r s t u v w x y z  . . . .
    #chunked
    a b c d e
    f g h i j
    k l m n o
    p q r s t
    u v w x y
    z . . . .