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


by gods
on Aug 24, 1999 at 22:42 UTC ( #267=perlfunc: print w/replies, xml ) Need Help??


See the current Perl documentation for grep.

Here is our local, out-dated (pre-5.6) version:

grep - locate elements in a list test true against a given criterion



This is similar in spirit to, but not the same as, grep(1) and its relatives. In particular, it is not limited to using regular expressions.

Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value consisting of those elements for which the expression evaluated to TRUE. In a scalar context, returns the number of times the expression was TRUE.

    @foo = grep(!/^#/, @bar);    # weed out comments

or equivalently,

    @foo = grep {!/^#/} @bar;    # weed out comments

Note that, because $_ is a reference into the list value, it can be used to modify the elements of the array. While this is useful and supported, it can cause bizarre results if the LIST is not a named array. Similarly, grep returns aliases into the original list, much like the way that a for loop's index variable aliases the list elements. That is, modifying an element of a list returned by grep (for example, in a foreach, map() or another grep()) actually modifies the element in the original list.

See also map for an array composed of the results of the BLOCK or EXPR.

Log In?

What's my password?
Create A New User
[shmem]: I'd really like to go to Canada to do some snow/ice sculptures/ building which I can't do here due to saggy winters
[stevieb]: I have six cabins ready for anybody who is interested, free, at any time of year. You just have to be prepared to do some travel. I'm a couple hours from the coast mountains, and five from the Rockies. shmem, you have a lot of clout...
[shmem]: one week with -10 C and all going "whoo! we're gonna die!"
[stevieb]: I wouldn't even check your passport ;)
[shmem]: next week again +12
[stevieb]: In all seriousness, I have an open invitation to my ranch. Few take it up, but I have traveled and can tour more of this country than anyone I know
[stevieb]: hahaha -10c? For 1.5 months it was a near consistent -36f
shmem lifts an eyebrow at stevieb. Be warned...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-03-22 00:19 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (272 votes). Check out past polls.