Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Index number repeating in for loop

by davido (Archbishop)
on Jun 28, 2013 at 22:13 UTC ( #1041359=note: print w/ replies, xml ) Need Help??


in reply to Index number repeating in for loop

...or simply...

print "'F' is at index: $_\n" for grep { $text[$_] eq 'F' } 0 .. $#text;

List::MoreUtils provides a sugary alternative, "indexes":

use List::MoreUtils qw( indexes ); # ... print "'F' is at index: $_\n" for indexes { $_ eq 'F' } @text;

This is a case where I prefer grep because what it's doing isn't unclear or awkward enough to justify the module (imho).


Dave


Comment on Re: Index number repeating in for loop
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-12-21 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls