Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re^3: Replacing values in an arrayby muba (Priest) |
on Jan 27, 2013 at 05:37 UTC ( [id://1015562]=note: print w/replies, xml ) | Need Help?? |
map and grep can be an intimidating functions, but quite useful once you understand them. Let's consider grep first, since this one is a little simpler to grasp — or at least, that was my experience. grep takes two arguments, the second being a list of elements to work with, the first being the work that you want to have done on that list. You can specify that as a BLOCK, a code reference, or just the name of a function. grep then loops over the list, aliasing $_ to each element in turn, and calls the given piece of code. Then it returns every element for which the code returned a true value.
See what happens? grep returns the elements of the list you gave it, for which the piece of code returns true. The non-grep equivalents would be:
Now, map is pretty similar, except that it allows you to change the elements:
Of course, these are just the basics — the range of things you can do with them is astonishing. I hope this helps you along.
In Section
Seekers of Perl Wisdom
|
|