Basic knowledge points for getting started with Perl

Source: Internet
Author: User

My personal link:Http://www.wachnew.com

 

The following are some of the knowledge points I recorded when learning Perl for later viewing. The following code is a program file that can be successfully run on the Perl parser.

 

#! /Usr/bin/perl-W

 

# Knowledge Point 1: S/matching mode string/replacement string /;
# S // returns true after the replacement is successful, otherwise false
$ _ = "Hello world, again ..";
S/Hello/Nice/; # Replace hello with nice
Print "$ _/N ";

S/s (/W +)/A Man's $1 /;
Print "$ _/N ";

$ _ = "Green scaly din20.ur ";
S /(? <Name1>/W + )(? <Name2>/W +)/$ + {name2}, $ + {name1}/; # use a hash value to capture and replace the green and scaly positions.
Print "$ _/N"; # changes to scaly, green din1_ur

# Replace at the starting position
S/^/huge, //; # changes to huge, scaly, green din1_ur
Print "$ _/N ";

S/,. * reen //; # Empty replacement, changed to huge din1_ur
Print "$ _/N ";

S/green/red/; # The matching fails.
Print "$ _/N ";

 

# Knowledge Point 2: S // G. If G is added, it indicates global replacement. For example:
$ _ = "Home, sweet home ";
S/home/cave/g; # Add g to global
Print "$ _/N"; # changes to cave, sweet cave

# Reduce any consecutive blank space to a single blank space
$ _ = "You are right, are you! ! ";
S/S + // G; # Add g to global
Print "$ _/N ";

# Delete the white space at the beginning
S/^/S + |/S + $ // G; # Delete the white space at the beginning and end
Print "$ _/N ";

 

# Knowledge Point 3: Different delimiters to be replaced include: S //, s ###, or s [] {}, s {} [], S (){}, s {} () and so on

 

# Knowledge Point 4: case-sensitive Conversion
#1:/u can be converted to uppercase
#2: Convert/L to lowercase
#3: Impact of case-sensitivity conversion after/E
#4: only the first character after/L and/u is affected.
#5: Use/u/L to convert the first one to uppercase at the same time, and convert the last one to lowercase.
#6: Use/L/u to convert the first one to lowercase, and convert the last one to uppercase.
$ _ = "I saw Barney with Fred .";
S/(Fred | Barney) // U $1/GI; # Replace the case-insensitive Fred and Barney with the overwrite Fred and Barney globally, that is, I saw Barney with Fred.
Print "$ _/N ";
S/(Fred | Barney) // L $1/GI; # Replace Fred and Barney in lower case, that is, I saw Barney with Fred
Print "$ _/N ";
S/(/W +) with (/W +) // U $2/e with $1/GI; # Replace Fred and Barney, however, the first one is the lower case after the upper case.
Print "$ _/N"; # I saw Fred with Barney
S/(Fred | Barney) // U $1/GI; # change the first letter of Fred or Barney to uppercase, And the rest remain unchanged
Print "$ _/N"; # I saw Fred with Barney
S/(Fred | brney) // U/L $1/GI; # change the first letters of Fred and Barney to uppercase, and the remaining letters to lowercase.
Print "$ _/N"; # I saw Fred with Barney

 

# Knowledge Point 5: The split operator (separated into substrings Based on separators and saved in the list) is as follows:
My @ list = Split/:/, "A: B: C ";
Print "There are: @ list/N"; # obtain "A", "B", "C"
@ List = Split/;/, "ABC; EF; G; H ";
Print "There are: @ list/N"; # Get "ABC", "Ef", "", "g", "H"

 

# Knowledge Point 6: Join function, which does not use the mode. Its function is opposite to that of split, as shown below:
# Function usage: Join $ jiao_shui, @ sub_string
# The Sub-string list must contain at least two elements; otherwise, the glue cannot be coated.
My $ _ = join ":", @ list; # The first parameter of join can be understood as glue, which is connected to each element in the following list one by one.
Print "$ _/N ";

# The following is an example of the cooperation between join and split. A string is first split Based on the separator, and then connected using another separator.
@ List = Split/:/, $ _;
$ _ = Join "-", @ list;
Print "$ _/N ";

 

# Knowledge point 7: m of the List context //
$ _ = "Hello there, neighbor! ";
My ($ first, $ second, $ third) = M/(/S +), (/S + )/;
Print "$ first, $ second, $ third/N ";

My $ text = "Fred dropped a 5 ton granite block on M4. slate ";
My @ words = ($ text = ~ M/([A-Z] +)/Gi); # bind $ test as the matching object, and save the matching result to the list @ words.
Print "there is: @ words/N ";

 

# Knowledge point 8: Pay attention to non-Greedy quantifiers +? *? ?? {5, 10 }? {8 ,}? And so on

# Knowledge Point 9: Cross-row pattern matching: with the pattern matching modifier/M (understood as multiple lines)
$ _ = "I'm much better/nthan Barney is/NAT bowling,/nwilma./N ";
Print "found 'wilm' at start of line/N" If/^ Wilma/B/IM;

# Knowledge point 10: string processing search index ($ source, $ sub_string, $ no ),The meaning is that the first occurrence of the substring $ sub_string needs to be found in the $ source string,
# If yes, a specific position is returned. Otherwise,-1, $ no indicates the start position of the search. If the third parameter $ no is omitted, the search starts from the start position 0.
# Rindex ($ source, $ sub_string, $ no): it is basically the same as the index, but it indicates to look forward from the end, and $ no indicates to limit the maximum position returned.
My $ stuff = "howdy world! ";
My $ where1 = index ($ stuff, "W"); # $ where1 = 2
My $ where2 = index ($ stuff, "W", $ where1 + 1); # $ where2 = 6
My $ where3 = index ($ stuff, "W", $ where2 + 1); #-1 is returned if no value is found.
Print "$ where1, $ where2, $ where3/N ";

My $ Fred = "yabba dabba doo! ";
$ Where1 = rindex ($ Fred, "Abba"); # $ where1 = 7
$ Where2 = rindex ($ Fred, "Abba", $ where1-1); # $ where2 = 1
$ Where3 = rindex ($ Fred, "Abba", $ where2-1); # $ where3 =-1
Print "$ where1, $ where2, $ where3/N ";

 

# Knowledge point 11: Use substr to process substrings
# Format: $ part = substr ($ string, $ initial_position, $ length );
# Parameter: $ string: string to be processed
# $ Initial_position: $ start position in string
# $ Length: the total length of the substring calculated from the starting position
# Return Value: Get the substring saved to $ part.

 

My $ mineral = substr ("Fred J. Flintstone", 8, 5); # The value is "Flint"
My $ rock = substr "Fred J. Flintstone", 1000; # The value is stone, although it cannot contain characters
My $ out = substr ("some very long string",-3, 2); # $ out = "in", the last position is-1, so-3 is "I"
Print "Mineral = $ mineral, rock = $ rock, out = $ out/N ";

My $ long = "some very long string ";
My $ right = substr ($ long, index ($ long, "L"); # obtain long string
Print "$ right/N ";

My $ string = "Hello, world! ";
Substr ($ string, 0, 5) = "goodbye"; # $ string: Goodbye, world!
Print "$ string/N ";

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.