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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Grep could have been called "filter" in array context.
Most common form is @output = grep{anon_sub_returning_True_False}@input;
This code:
#!/usr/bin/perl -w use strict; my @a = qw( a b c d ); my $i=0; my @b = grep { push(@a,'!') if !$i++; 1 } @a; print "@b\n"; print "@a\n"; __END__ output: a b c d a b c d !
Means take each item in @a and put it into the anon grep subroutine. If that subroutine returns true, then move that item to @b. Absent an explicit return statement, the return value of a Perl sub is the last statement. In this case it is "1", or always true. So this is the same as: @a=@b; as far as grep{} is concerned. This push statement gets executed but has no bearing upon the action of grep{}.

So, YES grep{} is meant to process/filter lists! I would argue that putting this push statement inside the grep is a weird thing to do.

grep does have a scalar context also! This is useful in some cases, here this is just a "dumb" example.

my $num = grep{1}@a; print $num; ##prints 5 (num of elements in @a) ie, abcd!
But this can be used to say count number of matches in @a.
$num = grep {/some_regex/}@a;
But of course most common use would be @a=grep{/regex/}@b; which moves any item from @b which matches regex to @a.

Update:Perl will not allow the input list to be modified within the grep{} subroutine. This could create a circular endless loop and that just won't happen.


In reply to Re^3: (Mis)Understanding <c>grep...eq $_<c> by Marshall
in thread (Mis)Understanding grep...eq $_ by ww

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2022-08-12 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?