Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Comparison Chart for Perl programmers - list functions
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.


functiondesciption of actionelement countelements altered?Output element order
grepFilters incoming elements and copies those which match a criteria to the output list"n" elements in, 0 to "n" elements outoutgoing elements are exact copies of incoming elementsoutgoing elements are in same order as incoming elements
mapPerforms an operation on each incoming element and writes the result to the output list"n" elements in, "n" elements outoutgoing elements are the result of an operation on incoming elementsoutgoing elements are in same order as incoming elements
sortRe-orders the incoming elements and writes the result to the output list"n" elements in, "n" elements outoutgoing elements are exact copies of incoming elementsoutgoing elements are in a different order to the incoming elements
reverseWrites the incoming elements to the output list in reverse order"n" elements in, "n" elements outoutgoing elements are exact copies of incoming elementsoutgoing 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 08:00:46)

 
Associated topics are indexed under
P208 - Perl - Lists

Back to
Just provide a room and the students
Previous and next
or
Horse's mouth home
Forward to
Too technical?

Some other Articles
Tcl sandwich - lists in Tcl
Network Camera
Colour for access
Too technical?
Comparison Chart for Perl programmers - list functions
Just provide a room and the students
Perl - redo and last without a loop
Certification schemes
Please tell us
Too many Perls
1693 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho