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

Loop problem

Status
Not open for further replies.

chrismassey

Programmer
Aug 24, 2007
264
GB
Hello,

I have a script which performs 3 tasks.

1) While the counter = less than 100, print the counter value then + 1

2) While the counter = more than 0, print the counter value then - 1

3) While the counter = more than 0, print the counter value then - 1, (skip the number 7)


The first 2 tasks work fine, but the last (3) isn't printing anything. The demo is available at
Code:
#! /usr/bin/perl
use strict;
use CGI ':standard';

## Declare variables
my ($counter);

print "Content-type: text/html\n\n";

## Loop 1 
$counter == 0;
print "<p><font face=arial size=3>While the counter = less than 100, print the counter value then + 1</font><p>";
while ($counter < 100) {
print "<font face=arial size=3>$counter, </font>";
$counter = $counter + 1;
}

## Loop 2
$counter == 100;
print "<p><font face=arial size=3>While the counter = more than 0, print the counter value then - 1</font><p>";
while ($counter > 0) {
print "<font face=arial size=3>$counter, </font>";
$counter = $counter - 1;
}

## Loop 3
$counter == 100;
print "<p><font face=arial size=3>While the counter = more than 0, print the counter value then - 1, (skip the number 7)</font><p>";
while ($counter > 0) {
if ($counter == 7) {
print "<font face=arial size=3>Skip</font>";
}
else {
print "<font face=arial size=3>$counter, </font>";
}
$counter = $counter - 1;
}

## End

Thanks, Chris
 
Change all your
$counter == 0;
to
$counter = 0;

It's just coincidence that the first two are working.. because on the first one you count up.. then you count down :)

Basically by the 3rd loop $counter is = to 0;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thank you very much travs, it works fine now!

I understand now that using == was a silly mistake, because you never give a variable a value using ==. I only use == in conditions.

Thanks alot :)

Chris
 
== for comparing values
= for assigning values

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top