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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

annoying for loop error 2

Status
Not open for further replies.

fortytwo

Technical User
Apr 18, 2000
206
GB
Hi, I have this for loop and it is looping three times:
[tt]
for ($i = 0, $i <= $noinsection, $i++)
{
print $i;
}[/tt]

even though $noinsection is 1. $i prints as 1 each time.

Anyone got any ideas?
 
You're using commas where you should be using semicolons. Try this instead:

for ($i = 0; $i <= $noinsection; $i++)
{
[tab]print $i;
}


Regards

 
Hi, Thanks for that. I guess because I didn't get an internal server error I thought the error lay elsewhere.

One thing, how did the first code section only run three times then exit?
 
I think perl uses your for-head as a list. The list
Code:
($i = 0, $i <= $noinsection, $i++)
has three elements and is evaluated before entering the loop. $i is set to 0 and incremented at last resulting to 1. The loop iterates 3 times because the list has 3 elements. The current value of $i (1) is printed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top