Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

problem with endless loop

by Anonymous Monk
on Jul 14, 2003 at 18:57 UTC ( [id://274099]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Greetings ,
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.
$table_one .= words(\@data_one); $table_two .= words(\@data_two); $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
while ($count < $elements) { print ("we are in MAIN loop\n"); while ($tr++ < $cols) { $test .= "<tr>";print ("we are in TR loop\n"); my $tc=0; while ($tc++ < $rows) { $test .="<td width='25%'align='left'>".(@{$ref}[$count]).'</td>'."\n"; print ("we are in tc loop\n");$count++ } # MAIN loop } $test .= "</tr>"; # tr loop } # td loop $test .= "</tr>"; } else {#do something else}

Replies are listed 'Best First'.
Re: problem with endless loop
by tilly (Archbishop) on Jul 14, 2003 at 19:30 UTC
    $tr looks like it is a global variable. Once it is bigger than $cols you will enter the main loop, skip the TR loop, not increment $cols and do the same..endlessly.

    strict.pm would have caught this typo. Also you are less likely to get into endless loops, off by one, and so in if you use the Perlish foreach style of looping:

    while ($count < $elements) { # Do you use $tr? If not, then don't bother declaring it... foreach my $tr (1..$cols) { $test .= "<tr>"; foreach my $tc (1..$rows) { $count++; # Do stuff } } }
Re: problem with endless loop
by swkronenfeld (Hermit) on Jul 14, 2003 at 19:31 UTC
    I downloaded your code, and couldn't spot the problem for certain (since I don't have @data_one, two, etc to look at)

    Where you print out "We are in the MAIN loop", try printing out $count and $element. Since it doesn't look like $element is increasing anywhere, this should help you figure out why $count isn't ever exceeding $element.

    Maybe $element isn't set correctly above? Or maybe you just have an OBOE (off by one error) with $count? I'd have to see more code to be sure...

    Hope this helped,
    Scot

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://274099]
Approved by jonnyfolk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 01:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found