[ILUG-Webdev] Perl: counter for three columns
adam beecher
lists at beecher.net
Fri Dec 12 11:32:17 GMT 2003
Got it. Needed to check for a stop marker and reset the counter, and
increment /after/ this. Silly me, should have dropped down to pseudocode
earlier.
adam
# Columns hack by Thallophytic g0d <hostyle at csn.ul.ie> && Me
my $count = $#channels + 1;
my $counter = 0;
my $columns = 3;
my $stop = int(($count / $columns) + .5);
my $width = int((100 / $columns) + .5);
# Open the table
to_browser(qq{ <br /><table width="100%"><tr valign="top">\n <td
width="$width%">});
# get the user's window target preference (which controls
# whether links should open up in the current or a new window.
my $link_target = get_setting( "user_link_target" );
# for each channel, parse and display.
foreach my $channel (@channels) {
if ( $counter == $stop ) {
to_browser(qq{ </td>\n <td width="$width%">});
$counter = 0;
}
$counter++;
<?php
$s=array(74,65,112,104,112,72,32,59,45,41);
for($i=0;$i<count($s);$i++){echo'&#'.$s[$i].';';}
?>
> -----Original Message-----
> From: adam beecher [mailto:lists at beecher.net]
> Sent: 11 December 2003 17:41
> To: WEBDEV
> Subject: RE: [ILUG-Webdev] Perl: counter for three columns
>
>
> Thanks eh... Thallophytic. That didn't work, I ended up with a
> rake of columns. With a little tweaking and twiddling, I managed
> to get it to the right number of columns with
> (($blah1%$blah2)==0), however I'm getting odd results now. For
> example, I'm getting the smaller number of feeds in the first
> column, whereas it should be in the last. Logic is pissing me
> off, I'm going drinking now! This is the latest code I have, but
> I have no idea how far I got with it... :)
>
>
> # Columns hack by Thallophytic g0d <hostyle at csn.ul.ie>
> my $count = $#channels + 1;
> my $counter = 0;
> my $columns = 4;
> my $stop = int(($count / $columns) + .5);
> my $width = int((100 / $columns) + .5);
>
> # Open the table
> to_browser(qq{ <br /><table width="100%">$stop\n <tr
> valign="top">\n <td width="$width%">});
>
> # get the user's window target preference (which controls
> # whether links should open up in the current or a new window.
> my $link_target = get_setting( "user_link_target" );
>
> # for each channel, parse and display.
> foreach my $channel (@channels) {
>
> $counter++;
>
> if ( ($counter % $stop) == 0 ) {
> to_browser(qq{ </td>\n <td width="$width%">});
> }
>
>
>
> <?php
> $s=array(74,65,112,104,112,72,32,59,45,41);
> for($i=0;$i<count($s);$i++){echo'&#'.$s[$i].';';}
> ?>
>
>
> > -----Original Message-----
> > From: Thallophytic g0d [mailto:hostyle at csn.ul.ie]
> > Sent: 11 December 2003 15:59
> > To: adam beecher
> > Cc: WEBDEV
> > Subject: Re: [ILUG-Webdev] Perl: counter for three columns
> >
> >
> > 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