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 @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";
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#!/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";
to print out the close table row tag like it does with the first script for the newlines.} elsif ($rows == $row_count) { $row_count = 0; print " <td width=\"100%\">\n"; print " number: [$i]\n"; print " </td>\n\n </tr>\n\n";
Edit kudra, 2002-01-10 Changed title
In reply to Loop counter only works for 2 by rendler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |