Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by ikegami (Patriarch)
on Feb 25, 2009 at 13:10 UTC ( #746252=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^5: (Mis)Understanding "works"
by tye (Sage) on Feb 25, 2009 at 15:27 UTC

    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.

        Frankly, that is more bizarre. You jumped from "grep works on lists and arrays" to "grep works on lists in the normal way and does something completely different to arrays"?

        But if your worry was that someone rather magically thought that grep on an array removes things from the array, then why did you demonstrate a lack of optimization rather than demonstrating that grep doesn't modify the array?

        In any case, I don't think your point was well understood (which doesn't surprise me). I think you would have been more helpful had you responded differently to the slightly redundant but not incorrect inclusion of "and arrays" which likely shows a leak of or bow to the common false dichotomy between "list" and "array" in Perl. Rather than elevate this false dichotomy to the point of "works on arrays" means "does something /different/ with arrays", you should have just noted that "grep works on lists. an array can be used as a list". That is, push back against the false dichotomy.

        grep works on lists. So, of course, it also works on arrays (which are a type of list).

        Bizarre consequences of optimizations of foreach seem particularly unenlightening to, more distracting from that point.

        - tye        

        Update:posted this my mistake at wrong reply level. I am agreeing with Tye and showing an example of how to use grep on "arrays".

        I don't know exactly what is meant by "array" in a Perl context. There are lists and lists of lists of lists(LoL) and of course lists of lists of lists(LoLoL), Lists of Hashes (LoH), etc. grep works great on all these things! If you mean that a LoL is an "array" then here is one example of how to use grep on something like that:

        #!/usr/bin/perl -w use strict; #remove "rows" that don't have a value greater than 75 my $value =75; my $rLoL = [ [78,43,87], [64,73,72], [99], [65,67,71] ]; @$rLoL = grep { (grep {$_ > $value}@$_)>=1}@$rLoL; foreach my $ref (@$rLoL) { print "@$ref\n"; } #prints #78 43 87 #99 #the inside grep above is used in a scalar context. #It basically says "do we want to keep this list or not?" #outside grep passes list ref to output based upon that #true/false decision. #grep{} works with anything that can be reduced to a #yes/no question #@LoH = grep{keys (%_) > 2}@LoH #removes hashes from List of Hashes that have fewer #than 3 keys.

Log In?
Username:
Password:

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

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












    Results (59 votes). Check out past polls.

    Notices?