Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: loopy trouble

by jeffa (Bishop)
on Jan 07, 2016 at 21:31 UTC ( #1152252=note: print w/replies, xml ) Need Help??


in reply to loopy trouble

First you have to answer the question, is the amount of times to loop going to be known ahead of time or will it depend on some condition being met down the road? If the former, you generally use for loops, and you generally use while loops for the latter.

# find sum of first 100 digits my $sum = 0; for my $i (0 .. 99) { $sum += $i; }

# print lines from a file handle and stop when done while (my $line = <FILE>) { print $line; }

If you need to break out of a loop, you can use the last keyword

for (0 .. 99) { print "$_\n"; last if $_ == 42; }

Hope this helps!

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2019-07-18 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?