[ILUG-Webdev] Perl: counter for three columns
Thallophytic g0d
hostyle at csn.ul.ie
Thu Dec 11 15:58:42 GMT 2003
On Thu, 11 Dec 2003, adam beecher wrote:
>
> # Hack to create two columns. Nasty perl but it's been a while.
> my $count = $#channels + 1;
> my $half = int($count / 2) + 1;
> my $counter = 0;
>
> # Open the table
> to_browser(qq{ <br /><table width="100%"><tr valign="top"><td
> width="50%"> });
>
> # for each channel, parse and display.
> foreach my $channel (@channels) {
> $counter = $counter + 1;
> if ( $counter == $half ) {
> to_browser(qq{ </td><td width="50%"> });
> }
>
>
> I'd like to change this to use three columns, which I guess I could do by
> dividing by three and adding one like yerman does above (doesn't perl have a
> modulus operator?), and then multiplying by two and yadda yadda, but I'd
> like to make a $columns var that I can adjust. Any hints for me?
Completely OTOH and untested, something like the following should do it.
my $columns = 3;
my $counter = 0;
my $width = 100 / $columns;
foreach my $channel (@channels) {
$counter++;
if ( $counter % $columns ) { # if there is a remainder
# add another cell
to_browser(qq{ </td><td width="$width%"> });
} else { # no remainder, end our row
to_browser(qq{ </tr><tr> });
}
}
Obviously the </tr><tr> isn't ideal ... but you should get the drift of it
More information about the Webdev
mailing list