Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Loop counter only works for 2

by rendler (Pilgrim)
on Jan 09, 2002 at 11:13 UTC ( #137400=perlquestion: print w/ replies, xml ) Need Help??
rendler has asked for the wisdom of the Perl Monks concerning the following question:

Okay first of all I started by making a prototype with this code.
#!/usr/bin/perl -w use strict; my @array = qw(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 +9 20); my $rows = 2; my $row_count = 0; my $array_total = 7; print "[START]\n"; for(my $i = 1; $array_total >= $i; ++$i) { if ($row_count == 0) { $row_count += 2; print "$array[$i] "; } elsif ($i == $array_total) { print "$array[$i]"; } elsif ($rows == $row_count) { $row_count = 0; print "$array[$i]\n"; } else { ++$row_count; print "$array[$i] "; } } print "\n[END]\n";
Which works fine when you change $rows or $array_total, then when I went to implement that into my code I came up with something like this
#!/usr/bin/perl -w use strict; my $total_sites = 7; my $row_count = 0; my $rows = 2; print "<table border=\"1\" width=\"100%\" cellpadding=\"3\" cellspacin +g=\"3\">\n\n"; for(my $i = 1; $total_sites >= $i; ++$i) { if ($row_count == 0) { $row_count += 2; print " <tr valign=\"top\">\n <td width=\"100%\">\n"; print " number: [$i]\n"; print " </td>\n\n"; } elsif ($i == $total_sites) { print " <td width=\"100%\">\n"; print " number: [$i]\n"; print " </td>\n\n"; } elsif ($rows == $row_count) { $row_count = 0; print " <td width=\"100%\">\n"; print " number: [$i]\n"; print " </td>\n\n </tr>\n\n"; } else { print " <td width=\"100%\">\n"; print " number: [$i]\n"; print " </td>\n\n"; } } print " </tr>\n\n</table>\n";
The problem is if $rows is anything other than 2 it doesn't work, I can't figure out why it's not doing the block
} elsif ($rows == $row_count) { $row_count = 0; print " <td width=\"100%\">\n"; print " number: [$i]\n"; print " </td>\n\n </tr>\n\n";
to print out the close table row tag like it does with the first script for the newlines.

Edit kudra, 2002-01-10 Changed title

Comment on Loop counter only works for 2
Select or Download Code
Re: Just plain dumb
by snowcrash (Friar) on Jan 09, 2002 at 12:26 UTC
    in your example the variable $row_count can only have two possible states, either 0 or 2. increment the variable every time you iterate the loop to set it to the actual row you have currently or use something more compact like this:
    #!/usr/bin/perl use warnings; use strict; my $total_sites = 20; my $rows = 4; print qq| <table border="1" width="100%" cellpadding="3" cellspacing="3">\n <tr>\n|; for my $i (1 .. $total_sites) { print "\t<td>number: [$i]</td>\n"; print "</tr>\n<tr>\n" unless ($i % $rows || $i == $total_sites); } print "</tr>\n\n</table>\n";

    snowcrash
      Wow that works great thanks sooo much :)
Re: Just plain dumb
by jonjacobmoon (Pilgrim) on Jan 09, 2002 at 12:31 UTC
    Nothing dumb about this. Just inexperience.

    Frankly, though, you have hit one of my great pet peeves. Embedding printing (ie. heredocs, html, forms) in the perl only leads to serious maintenance issues. Good job security, but bad style.

    At the very least, look into using CGI.pm; however, I recommend that you use a module specific to template processsing. I prefer Template Toolkit but there are several of them. If you are not familiar with what templates are, its a good time learn. You won't regret it.

    (Plus, don't you just hate having to escape all the quotes. Use qq(foo) instead.))


    I admit it, I am Paco.
      I usually use blah <<"END; Is there any difference between that and qq?
        Not much, but you can use them in different ways.

        e.g. you can do this:

        print qq{the "cat" sat {furrily} on the 'so-called' mat};
        without having to escape any of the quotes, or the brackets.

        The same is true of a here document (e.g. <<"END") of course, in fact that's more adaptable because it can contain anything at all (except \nEND, obviously), but it takes more than one line.

        hth,
        andy.

Re: Just plain dumb
by CharlesClarkson (Curate) on Jan 09, 2002 at 12:38 UTC
    The problem is if $rows is anything other than 2 it doesn't work, I can't figure out why it's not doing the block
        } elsif ($rows == $row_count) {
    to print out the close table row tag like it does with the first script for the newlines.

    On the first pass $row_count is incremented to 2. After that it always remains 2. So the above elsif will only be executed when $rows is set to 2.




    HTH,
    Charles K. Clarkson

    PS Why are all the columns (<TD></TD>) set to 100% width? If the table has 2 columns, it's 200% wide.


      As for the TD tags they're set to 100% because of the fact that you can specify how many blocks there are in a row I want them to all have the same size. Which means that if there's 2 blocks in the row at 100% the blocks each take 50% of the page. Works a treat in opera but netscape 4.78 doesn't seem to like it very much, I have an example of what I'm trying to do at http://geocities.com/rendler/news.html
        Set the <table> to your desired width, and the browser will spread the available space evenly if not told otherwise.
Re: Just plain dumb
by Aristotle (Chancellor) on Jan 09, 2002 at 23:41 UTC

    Uhm.. oh dear :---)

    From just a quick glance at your code:

    • Arrays start at 0, not at 1 (you can do the latter, but hell will have you for it, so stay away)
    • You should nearly always use foreach (@list) rather than for($i;$c;$n) - even if you really need a counter,

      foreach my $i (0 .. 100)
      is better than the corresponding for(;;) construct. It is more efficient (yes, it is), and it removes fencepost errors - you cannot make a mistake on the condition that checks whether you have arrived on your last iteration. With the for(;;) form it is common to overlook nasty special cases that make the loop run once too much or too little.

      Having said that, it is even better to do

      foreach my $element (@array) { print $element; }
      than
      foreach my $i (0 .. 9) { print $array[$i] }
      If you really really need the $i for other purposes besides accessing the @array, it is often still better to do something like
      my $i = 0; foreach my $element (@array) { print "$i $element"; $i++ }
      or maybe
      my $i = -1; foreach my $element (@array) { $i++; print "$element $i"; }

    You should really check out Mark-Jason Dominus's excellent program repair shop series on www.perl.com. There is some very helpful advice there to get you started on writing better Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://137400]
Approved by root
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: (8)
As of 2014-08-21 03:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (127 votes), past polls