Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: (Mis)Understanding <c>grep...eq $_<c>

by Marshall (Canon)
on Feb 25, 2009 at 08:22 UTC ( #746191=note: print w/replies, xml ) Need Help??


in reply to Re^2: (Mis)Understanding <c>grep...eq $_<c>
in thread (Mis)Understanding grep...eq $_

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.

Replies are listed 'Best First'.
Re^4: (Mis)Understanding <c>grep...eq $_<c>
by ikegami (Patriarch) on Feb 25, 2009 at 13:10 UTC

    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.

    Obviously processes/filters lists. Obviously a weird thing to do. Jethro said grep also works on arrays, not just lists, and I was demonstrating that it only works on lists.

      grep works just fine on arrays (and other lists). You live in a bizarre world if you interpret "grep works on arrays" to mean "grep is optimized to do something special if passed a single array similar to how foreach (in some versions of Perl) is optimized" (something that you can only detect by doing "weird" things). *shrug*

      - tye        

        He didn't say list including arrays. He said list and arrays. What's special about grepping an array? Does it remove elements from the array? If he had said yes, then my next question would have been to ask what grepping an array does and how to do it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2022-01-19 20:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (56 votes). Check out past polls.

    Notices?