The map function of Perl

Source: Internet
Author: User
Tags scalar



Source:



Use of the map function of Perl:






Grammar



Map EXPR, LIST of which there is,



Map BLOCK LIST None of this,



Define and use



Executes expr or block on each element in the list, returning a new list. For each of these iterations, the values of the elements of the current iteration are saved in $_.






return value



If the return value is stored in a scalar scalar, the number of elements in the array is returned on behalf of Map ();



If the return value is stored in the list, it represents an array of map () functions;






Example 1 (Capitalize the first letter of the word)


 
 
 1 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#!/usr/bin/perl -w
 2 
 3 @myNames = (‘jacob‘, ‘alexander‘, ‘ethan‘, ‘andrew‘);
 4 @ucNames = map(ucfirst, @myNames);
 5 $numofucNames = map(ucfirst, @myNames);
 6 
 7 foreach $key ( @ucNames ){
 8  print "$key\n";
 9 }
10 print $numofucNames;


Result is


 
Jacob
Alexander
Ethan
Andrew
4





Example 2 (get all the words contained in the title and convert to uppercase)


 
 
1 my@books = (‘Prideand Prejudice‘,‘Emma‘, ‘Masfield Park‘,‘Senseand Sensibility‘,‘Nothanger Abbey‘,
2 ‘Persuasion‘,  ‘Lady Susan‘,‘Sanditon‘,‘The Watsons‘);
3 
4 my@words = map{split(/\s+/,$_)}@books;
5 my@uppercases = map uc,@words;
6 foreach $upword ( @uppercases ){
7  print "$upword\n";
8 }


The result is (the input array and the output array of the Perl map function are not necessarily long, and after the split has worked, of course the length of the @words is longer than the @books.) )


 
PRIDEAND
PREJUDICE
EMMA
MASFIELD
PARK
SENSEAND
SENSIBILITY
NOTHANGER
ABBEY
PERSUASION
LADY
SUSAN
SANDITON
THE





Example 3 (extract the extra 2 digits to the new list)


 
1 my @buildnums = (‘R010‘,‘T230‘,‘W11‘,‘F56‘,‘dd1‘);
2 my @nums = map{/(\d{2,})/} @buildnums;
3 foreach $num (@nums){
4   print "$num \n"
5 }


Results


 
010 
230 
11 
56 





Instance 4 matches the scalar and list context return values


 
 1 $a = ‘RRR32Sttt‘;
 2 @yy = $a=~/RRR.*ttt/;
 3 $numofyy = $a=~/RRR.*ttt/;
 4 print "@yy\n";
 5 print "$numofyy\n" ;
 6 print "$1";
 7 
 8 @yy2 = $a=~/(RRR).*(ttt)/;
 9 $numofyy2 = $a=~/(RRR).*(ttt)/;
10 print "@yy2\n";
11 print "$numofyy2\n";
12 print "$1 $2";


Result (the array or length returned after a regular expression match depends on whether or not the expression has () or the type of variable received)


 
1
1
RRR ttt
1
RRR ttt


whether there is a () variable type result received in an expression



No scalar forever 1 or 0



There are scalars forever 1 or 0



No list forever (1 or 0)



List with List of results






Perl's Map function


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.