iceraider has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Grep Fuunction
by Abigail-II (Bishop) on Jun 11, 2002 at 16:24 UTC | |
grep and map differ in the return values. The former will always returns a subset of the original list (the entire list is a subset as well). An element of the original list will be returned if the expression (or block) returned a true value when that element was processed. The return value of map is a list consisting of the actual return values of the expression or block. Abigail | [reply] [d/l] [select] |
by smitz (Chaplain) on Jun 12, 2002 at 10:13 UTC | |
But now I get it! One little paragraph, thank you so much! Maybe you should re-write all the perldocs ;-) SMiTZ | [reply] |
|
Re: Grep Function
by tadman (Prior) on Jun 11, 2002 at 16:28 UTC | |
Instead you could use a combination of map and grep: my @foo_bar = map { $_ - 1 } grep { $_ > 12 } @fooz;These constructs can be chained together and that is what makes them highly adaptable. Data flows right to left, though, so watch out. | [reply] [d/l] [select] |
|
Re: Grep Function
by joealba (Hermit) on Jun 11, 2002 at 16:56 UTC | |
Do a few Perlmonks Google searches, check out some of the Monks' code snippets, and you will find some great examples of how grep and map are your friends. Like these: Map Tutorial: The Basics Map and Grep | [reply] |
|
Re: Grep Fuunction
by swiftone (Curate) on Jun 11, 2002 at 17:06 UTC | |
There are many, many uses. Here are a few examples: From the perldoc on readdir(): Here grep was used to take the array from readdir() and filter out non-start-with-dot-files Here's one I use often: I might have an array of values, that, at some point, I need to do a series of checks to see if a value is in the array. It's inefficient to loop through the array multiple times. Likewise, we mostly need this data as an array, not a hash, this is just the exception. So we use map() to copy it to a hash: Now we can check for the existence of $foo in @array by checking $check_hash{$foo} for truth (or existence) Let's say you're doing a database update, but you have a variable number of fields. In fact, you have the fields as @fields. Let's also assume you have a CGI object in $q that has all the needed fields there (and taint-checked) Unrolling this, we take each value in @field, and make it instead the text "set (value) =?". We then join those elements to end up with a legit SQL statement. In the execute() call, we want the $q->param() value foreach element of field...so we use map() to get it. What if we didn't have $key defined in the above? I mean, what if $key was the name of the field that was the primary key, and the value was taken from the passed parameters? A slight modification that would use grep(): Now the Key field is only used where needed. (Note this code woudl break if key wasn't last. It would be more durable if the execute() call also grepped out the key, then explicitly added it to the end: (And one last caveat -- $key will be interpretted as a regex. For alphanumeric keys, that's not a problem.) Hope that helps, there are many more examples out there. In general, use grep when you need part of an array as an array, and use map when you need an array based off another list. If you need to actually execute code based on the contents of a list, you're better off using a loop. | [reply] [d/l] [select] |
| A reply falls below the community's threshold of quality. You may see it by logging in. | |