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

ranges

Status
Not open for further replies.

ErichTheWebGuy

Programmer
Jul 17, 2002
9
US
Ok, so if I wanted to do something like this:

Code:
for (1..9) {

 #some code

}

How can I tell which number is currently being worked around in my loop?
 
The code
Code:
for (1..9) {

 #some code

}
is equivalent to
Code:
my @array = (1..9);

foreach $_ (@array) { #for and foreach are synonymous

        print "$_ ";
}
This will print 1 2 3 .. etc.
In the for loop you posted, '$_' contains the current value of the iteration.

If you are a C programmer (or are used to C-style code) then a similar loop might look like
Code:
for ($_=1; $_ <= 9: $_++) {
     # Do some stuff
}
jaa
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top