Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: foreach only for a section of a list

by kennethk (Monsignor)
on May 04, 2012 at 15:01 UTC ( #968934=note: print w/ replies, xml ) Need Help??


in reply to foreach only for a section of a list

There are a few ways. You can use C-style For Loops:

for (my $i = 0; $i < @somewords -1; $i++) { print " $somewords[$i];\n" }
You can use Slices:

foreach $core (@somewords[0 .. $#somewords-1]) { print " ${core};\n" }
Or, in some cases, it makes sense to use Loop Control:

my $i = 0; foreach my $core (@somewords) { next if ++$i == @somewords; print " ${core};\n" }

TIMTOWTDI

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re: foreach only for a section of a list
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-09-16 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (47 votes), past polls