Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1041359]
help
Chatterbox?
[marioroy]: LA only $ and \ needed backslash, not ' and ".
[marioroy]: my @music_times = qx(find \$HOME/Music/ Albums -type f -iname '*.mp3' -exec mp3info -p "%S\\n" {} \\;);
[marioroy]: I wanted to come back and report on that.
[marioroy]: cheers

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2017-04-23 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (433 votes). Check out past polls.