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

grep! (My favorite <em>this</em> week, anyway...)

by Russ (Deacon)
on May 11, 2000 at 06:49 UTC ( [id://11102]=note: print w/replies, xml ) Need Help??


in reply to My favorite looping mechanism in Perl is:

Don't forget grep!

my @True; for my $Element (@Array){ if ($Element){ push @True, $Element; } }
becomes:
my @True = grep {$_} @Array;
To get every nth element from a list:
(Previously posted by me as Anonymous Monk...)
#(For every fourth element in @List) grep {not ++$i % 4} @List; #(To skip every fifth element) grep {++$i % 5} (1..50)
I suppose we could argue that grep isn't really intended as a looping mechanism, but map seems to be popular around here, and map is implemented on grep code (internally) so...

:-)

Russ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-16 20:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found