Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: for loops

by Willman023 (Scribe)
on Nov 07, 2002 at 16:30 UTC ( #211102=note: print w/replies, xml ) Need Help??

in reply to for loops

I think it would be useful to show an additional way the for loop can be used. Although the C-like for loop can do the same thing, I find myself using the one below more and more.

for $countdown (10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 'Liftoff!') { print $countdown, "\n"; } for $y (0..$x) { print $array[$y], "\n"; } for (@array) { s/value1/value2/ } # Substitues value2 for value 1.


Replies are listed 'Best First'.
Re^2: for loops
by TedPride (Priest) on Oct 11, 2004 at 20:07 UTC
    Mmm. For allows you control of the looping variable, which means you can make it skip backwards and forwards:
    for ($i = 0; $i < 40; $i++) { $i += 10 if (@arr[$i] eq 'jump'); }
    However, foreach covers pretty much every other situation. I haven't used for in quite some time now.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://211102]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2020-06-04 05:14 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (30 votes). Check out past polls.