Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with Endless Loop

Status
Not open for further replies.

calabama

Programmer
Feb 19, 2001
180
0
0
US
Hello ,

I have run into a problem while writing @arrays to a subroutine. I am concantenating the result from the subroutine words() to four different tables.

The first 2 lines return the correct results without any problem the second 2 lines produce an endless loop.


######################################
Code:
$table_one .= words(\@data_one);
$table_two .= words(\@data_two);
Code:
#$table_three .= words(\@data_three);
#$table_four .= words(\@data_four);

#######################################


The loop that is getting stuck is where the HTML for the tables is concantenated and later returned. I had added 3 print statements in the loop and the print statement that keeps looping is "we are in MAIN loop".

Thanks You for any help

Cal

#######################################

Code:
while ($count < $elements) {
Code:
print (&quot;we are in MAIN loop\n&quot;);
Code:
while ($tr++ < $cols) {
$test .=  &quot;<tr>&quot;;print (&quot;we are in TR loop\n&quot;); 
my $tc=0;
while ($tc++ < $rows) {

$test .= &quot;<td width='25%' align='left'>&quot;.(@{$ref}[$count]).&quot;</td>&quot;.&quot;\n&quot;;print (&quot;we are in tc loop\n&quot;);$count++
         } # MAIN loop
      } $test .=  &quot;</tr>&quot;;  # tr loop
  }      # td loop

    $test .=  &quot;</tr>&quot;;

} else {#do something else}
#######################################

In the begining
Let us first assume that there was nothing to begin with.
 
so -- $tc is never gt or eq to $rows

what is the value of $rows?

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
you might find that the proble could well lie with how you are arranging your rows and columns.

1) $elements contains the number of elements in the passed array ( scalar(@array) ) or the last elements index ( $#array )?
2) you are testing $tr against $cols and $tc against $rows, aside from referencing them the wrong way around, what are $cols and $rows set to?
3) your closing td loop actually closes a tr.
4) you don't appear to set tr (or is this ouside the code block above)?

However, the big problem you have is that

5) $count never gets incremented once you have passed the limit imposed by $cols. Thus gets stuck in an endless loop.

With a bit of code cleaning is there any reason why you can't say:

sub words {
my ($rows,$cols) = (10,4);
my ($tr,$tc) = (0,0);
my $test = '';

foreach (@_) {
last if($tr++ >= $rows);
$test .= &quot;<tr>&quot; unless($tc % $cols);
$test .= &quot;<td width='25%' align='left'>$_</td>\n&quot;;
$test .= &quot;</tr>&quot; unless($tc++ % $cols);
}
$test .= &quot;</tr>&quot; if($tc % $cols);

return $test;
}

Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top