grep v
map v
sort
Most languages support lists and / or arrays - and that includes Perl. In Perl, though, you can use functions such as grep, map, sort and reverse to operate on lists as a whole rather than having to loop through members of the list cell by cell.
function | desciption of action | element count | elements altered? | Output element order |
---|
grep | Filters incoming elements and copies those which match a criteria to the output list | "n" elements in, 0 to "n" elements out | outgoing elements are exact copies of incoming elements | outgoing elements are in same order as incoming elements |
map | Performs an operation on each incoming element and writes the result to the output list | "n" elements in, "n" elements out | outgoing elements are the result of an operation on incoming elements | outgoing elements are in same order as incoming elements |
sort | Re-orders the incoming elements and writes the result to the output list | "n" elements in, "n" elements out | outgoing elements are exact copies of incoming elements | outgoing elements are in a different order to the incoming elements |
reverse | Writes the incoming elements to the output list in reverse order | "n" elements in, "n" elements out | outgoing elements are exact copies of incoming elements | outgoing elements are "back to front" from the incoming elements |
It's a common misconception that grep is used only to filter incoming members against a regular expression - it CAN be (and that's the most common use and how it got its name), but it can also be used to perform another task, based on each member of the incoming list in turn being put into $_. The following sample program shows two uses of grep and two of map to illustrate the comments just made and parts of the table above.
# antonia Perl XML PHP Tcl/Tk MySQL
# barbara Tcl/Tk ASP Ruby Java
# cherry Perl Java Ruby MySQL
open (FH,"requests.xyz") or die "No requests.xyz file\n";
@stuff = grep (/^.[aeiou]/,<FH>); # Only want lines with vowel as 2nd letter
@m1 = grep((split)>5,@stuff); # Filter to remove short lines
print @m1;
print "============================\n";
@m1 = map(uc,@m1); # Change all lines to all upper case
print @m1;
print "============================\n";
@m3 = map(length()."\n",@m1); # Produce a list of line lengths
print @m3;
I ran this against a test data file of 52 lines (I've pasted the first three lines into the sample code above) and here are the results - you'll see that the only lines left by grep are those which comprise over 5 space separated fields, and have a lower case vowel as the second letter.
[localhost:~/dplp] graham% perl nq
hazel PHP Python Perl Ruby ASP
leane PHP Python ASP Perl Java
margaret XML Perl Ruby MySQL Tcl/Tk
petra XML Tcl/Tk ASP Perl Ruby
xena Java Perl PHP ASP XML
barry Python XML Java Perl PHP
============================
HAZEL PHP PYTHON PERL RUBY ASP
LEANE PHP PYTHON ASP PERL JAVA
MARGARET XML PERL RUBY MYSQL TCL/TK
PETRA XML TCL/TK ASP PERL RUBY
XENA JAVA PERL PHP ASP XML
BARRY PYTHON XML JAVA PERL PHP
============================
31
31
36
31
28
31
[localhost:~/dplp] graham%
There are
further examples of the use of functions such as grep (and push and pop and others too) available under our training note pages.
(written 2004-12-04, updated 2006-06-05)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P208 - Perl - Lists [28] Perl for breakfast - (2004-08-25)
[230] Course sizes - beware of marketing statistics - (2005-02-27)
[240] Conventional restraints removed - (2005-03-09)
[355] Context in Perl - (2005-06-22)
[463] Splitting the difference - (2005-10-13)
[560] The fencepost problem - (2006-01-10)
[622] Queues and barrel rolls in Perl - (2006-02-24)
[762] Huge data files - what happened earlier? - (2006-06-15)
[773] Breaking bread - (2006-06-22)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[968] Perl - a list or a hash? - (2006-12-06)
[1304] Last elements in a Perl or Python list - (2007-08-16)
[1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
[1703] Perl ... adding to a list - end, middle, start - (2008-07-09)
[1828] Perl - map to process every member of a list (array) - (2008-10-09)
[1917] Out of memory during array extend - Perl - (2008-12-02)
[1918] Perl Socket Programming Examples - (2008-12-02)
[2067] Perl - lists do so much more than arrays - (2009-03-05)
[2226] Revision / Summary of lists - Perl - (2009-06-10)
[2295] The dog is not in trouble - (2009-07-17)
[2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
[2813] Iterating over a Perl list and changing all items - (2010-06-15)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
[3870] Writing more maintainable Perl - naming fields from your data records - (2012-09-25)
[3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
[3939] Lots of ways of doing the same thing in Perl - list iteration - (2012-12-03)
[4609] Mapping an array / list without a loop - how to do it in Perl 6 - (2016-01-03)
Some other Articles
Tcl sandwich - lists in TclNetwork CameraColour for accessToo technical?Comparison Chart for Perl programmers - list functionsJust provide a room and the studentsPerl - redo and last without a loopCertification schemesPlease tell usToo many Perls