Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Perlplexation - foreach shoulda Known

by davido (Cardinal)
on Apr 13, 2012 at 16:23 UTC ( #964985=note: print w/replies, xml ) Need Help??


in reply to Re: Perlplexation - foreach shoulda Known
in thread Perlplexation - foreach shoulda Known

There is not. If you're reading a file $. contains the file's line number. But if you're iterating over elements in an array it may make sense in some situations to iterate over the index instead:

foreach my $idx ( 0 .. $#array ) { do_something($array[$idx]) unless $idx % 3; # Do something else that isn't filtered by iteration number. }

In your example, it's probably clearer to iterate over the list rather than the indices, but in some cases the code becomes clearer the other way.


Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2018-12-12 01:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (57 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 18:53 UTC) Welcome new users!