4.14. Finding All Elements in an Array Matching Certain Criteria4.14.1. ProblemFrom a list, you want only the elements that match certain criteria. This notion of extracting a subset of a larger list is common. It's how you find all engineers in a list of employees, all users in the "staff" group, or all the filenames you're interested in. 4.14.2. SolutionUse grep to apply a condition to all elements in the list and return only those for which the condition was true:
4.14.3. DiscussionThis could also be accomplished with a foreach loop:
The Perl grep function is shorthand for all that looping and mucking about. It's not really like the Unix grep command; it doesn't have options to return line numbers or to negate the test, and it isn't limited to regular-expression tests. For example, to filter out just the large numbers from an array or to find out which keys in a hash have very large values:
Here's something that sets @matching to lines from the who command that start with "gnat ":
Here's another example:
It extracts only those objects from the array @employees whose position method returns the string Engineer. You could have even more complex tests in a grep:
But at that point you may decide it would be more legible to write a proper loop instead. 4.14.4. See AlsoThe "For Loops," "Foreach Loops," and "Loop Control" sections of perlsyn(1) and Chapter 4 of Programming Perl; the grep function in perlfunc(1) and Chapter 29 of Programming Perl; your system's who(1) manpage, if it exists; Recipe 4.13
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|