In Perl, you can process whole lists (arrays) in single operations - and that's very efficient at run time as it avoids the needs for loops, and the needed for Perl's Virtual Machine to go back to the byte code (in effect source) for each member.
The
map function applies an opertion to each member of a list, and returns a list of the same length, with each element duely mapped. The input to the transform function is $_ - so with embedded calls to functions like
uc there's no need to specify it. But if you're going to take 1 off each member then you'll find $_ turn up in the code.
The
grep function applies an operation to each member of a list, and returns the member unaltered for inclusion in the output list if the result is a true value. Otherwise, the input member is not included in the output list. So the output list from grep is going to be shorter than the incoming list, but the members themselves won't be modified.
@numbers = (10,30,40,33,45,32,43,22);
# Subtract 1 of each member of @numbers
@numbers = map($_-1,@numbers);
# Return a list of all numbers that are possible days of the month
@digit = grep( $_ <=31 ,@numbers);
print ("@digit\n");
@people = ("tim","steve","JASPER","DaVeY");
# Force all input members to lower case, then capitalise the first letter
@people = map(ucfirst lc,@people);
print "@people\n";
(written 2007-08-23)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P669 - Perl - Data Munging [597] Storing a regular expression in a perl variable - (2006-02-09)
[1509] Extracting information from a file of records - (2008-01-16)
[1947] Perl substitute - the e modifier - (2008-12-16)
[2129] Nothing beats Perl to solve a data manipulation requirement quickly - (2009-04-14)
[2702] First and last match with Regular Expressions - (2010-04-02)
[3335] Practical Extraction and Reporting - (2011-06-26)
[3707] Converting codons via Amino Acids to Proteins in Perl - (2012-04-25)
[3764] Shell, Awk, Perl of Python? - (2012-06-14)
[4620] Perl 6 - a Practical Extraction and Reporting example! - (2016-01-11)
P208 - Perl - Lists [28] Perl for breakfast - (2004-08-25)
[140] Comparison Chart for Perl programmers - list functions - (2004-12-04)
[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)
[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
Perl for Larger Projects - Object Oriented PerlCustomer feedback - lifeblood of a businessWell House Manor - feature comparison against the old place!2008 course schedule - Perl, Python, PHP, Linux, Java Deployment, Ruby and moreFiltering and altering Perl lists with grep and mapTwo years of campaigning for a train serviceBusiness travel by train in the USATratum TechnologiesSome one line Perl tips and techniquesWhat do people look for on a hotel web site?