SPUG: Where is grep's return value?

Parr, Ryan Ryan.Parr at wwireless.com
Tue Sep 10 18:34:41 CDT 2002


if(my @good_stuff = grep(EXPR,LIST)) {}

Note that you can't do something like:
if(my @good_stuff = grep("FName",qw(FName MName LName))) {}

because the expression returns it's last evaluation, which is simply a
string, which is always true, so your list will be the full contents of the
grep'd list. Make sure you perform some kind of operations:

if(my @good_stuff = grep(/FName/,qw(FName MName LName))) {}
--or--
if(my @good_stuff = grep { /$KEY/ } qw(FName MName LName)) {}


=== perldoc -f grep ===

=item grep BLOCK LIST

=item grep EXPR,LIST

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
C<$_> to each element) and returns the list value consisting of those
elements for which the expression evaluated to true.  In 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 C<$_> is an alias to the list value, so it can be used to
modify the elements of the LIST.  While this is useful and supported,
it can cause bizarre results if the elements of LIST are not variables.
Similarly, grep returns aliases into the original list, much as 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 C<foreach>, C<map>
or another C<grep>) actually modifies the element in the original list.
This is usually something to be avoided when writing clear code.

See also L</map> for a list composed of the results of the BLOCK or EXPR.

-----Original Message-----
From: Orr, Chuck (NOC) [mailto:chuck.orr at attws.com]
Sent: Tuesday, September 10, 2002 3:28 PM
To: 'spug-list at pm.org'
Subject: SPUG: Where is grep's return value?


Hello,

I have something that looks like this:

if (grep("$KEY",EXISTING)){

In the event that this is true, I want to print the data that makes it true.
In other words, I want to print the line from EXISTING that contains the
pattern from the variable $KEY.  The problem I am having is figuring out
where that line is.  Is it stored in a variable I don't know about?  If not,
how can I obtain this data?  I realize this is a pretty basic question, but
I am having a tough time finding the answer and I need to get this done.
Any suggestions/help would be greatly appreciated.

Thanks,
Chuck Orr

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list