Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: foreach only for a section of a list

by kennethk (Abbot)
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" }


#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?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2016-02-09 19:19 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (323 votes), past polls