Perl primary tutorial [Day 3]

xiaoxiao2021-03-06  38

Conditional statements

Perl, of course, also supports the IF / THEN / ELSE statement, and below is:

IF ($ a)

{

Print "The string is not empty / n";

}

Else

{

Print "The String IS Empty / N";

}

Remember, the empty string is considered False, if $ A is "0", the result will be "empty".

Elsif can also be used in the conditional statement:

IF (! $ a) # The! is the not operator

{

Print "The String IS Empty / N";

}

Elsif (Length ($ a) == 1) # if Above fails, TRY THIS

{

Print "The String Has One Character / N";

}

Elsif (Length ($ a) == 2) # if That Fails, Try this

{

Print "The String Has Two Characters / N";

}

Else # now, Everything Has Faled

{

Print "The String Has Lots of Characters / N";

}

Note: There is indeed "E" in ELSIF. String match

One of the most useful features of Perl is

Its powerful string processing capability. The core is a rule expression (Regular Expression - Re) that is used by many other UNIX tools.

Rule expression

The rule expression is included in the slope and matches the = ~ operator. If the string The appears in the variable $ SENTENCE, the following expression is true:

$ SENTENCE = ~ / the /

RE is sensitive, if

$ SENTENCE = "The Quick Brown Fox";

Then the above match results are false. Operators! ~ When using "non-match", in the above example

$ SENTENCE! ~ / THE /

It is true, because the string the is not appeared in $ SENTENCE.

Special variable $ _

Conditional statement

IF ($ SENTENCE = ~ / Under /)

{

Print "We're Talking About Rugby / N";

}

Among us, we have one of the following two expressions:

$ SENTENCE = "UP and Under";

$ SENTENCE = "Best Winkles In Sunderland";

A message will be printed.

But if we assign this sentence to a special variable $ _, it will be easier to use. If so, we can avoid using matching and non-match operators, and the above examples can be written:

IF (/ under /)

{

Print "We're Talking About Rugby / N";

}

$ _ Variable is the default variable of many Perl operations, often used.

Other RE

There is a large number of special characters in RE, making them powerful, making them complicated. It is best to slowly use RE, and use it for them is an art.

Here are some special RE characters and their meaning:

. # Any Single Character Except a newline

^ # The beginning of the line or string

$ # The end of the line or string

* # Zero or more of the last character

# One or more of the last character

? # Zero or one of the last character? # Zar

Here are some examples of matches, should be added to /... /

T.e # t back by ANTHING FOLLOWED BY E

# This will match the

# TRE

# tle

# But not te

# tale

^ f # f at the beginning of a line

^ ftp # ftp at the beginning of a line

E $ # e at the end of a line

TLE $ # tle at the end of a line

und * # un folded by Zero or More D Characters

# This will match un

# UND

# UNDD

# Unddd (ETC)

. * # Any string without a newline. This is because

# The. matches Anything Except a newline and

# The * means zero or more of these.

^ $ # A line with nothing in it.

There are more usage. Square brackets are used to match any of these characters. In square brackets "-" indicates "Between", "^" means "not":

[QJK] # Either Q or J Or K

[^ QJK] # Neither q Nor J Nor K

[a-z] # anything from a to z inclusive

[^ a-z] # no lower case letters

[A-ZA-Z] # any letter

[A-Z] # any non-zero sequence of limited z z s

The above mentioned is basically enough, just refer to the reference:

The vertical line "|" means "OR", brackets (...) can be set:

Jelly | cream # Either Jelly or create

(eg | le) GS # Either Eggs or Legs

(DA) # Either da or Dada or Dadada OR ...

Here are some other special characters:

/ N # a newline

/ t # a tab

/ w # any alphapleric (word) Character.

# THE SAME AS [A-ZA-Z0-9_]

/ W # any Non-Word Character.

# THE SAME AS [^ A-ZA-Z0-9_]

/ d # any Digit. The Same as [0-9]

/ D # any non-Digit. The Same as [^ 0-9]

/ s # any Whitespace Character: Space,

# Tab, Newline, ETC

/ S # any non-whitespace Character

/ b # a word boundary, outside [] only

/ B # no word boundary

The characters like $, |, [,), /, /, if you want to reference them, you must add a backslash in front:

/ | # Vertical Bar

/ [# An open square bracket

/) # A closing parenthesis

/ * # An asseterisk

/ ^ # A carat symbol

/ / # A slash

// # a backslash

For example, we mentioned earlier, using RE is best slowly. Here are some examples, when you use them, you should /... /.

[01] # Either "0" or "1"

// 0 # a division by Zero: "/ 0"

// 0 # a Division by Zero with a space: "/ 0"

/// s0 # a division by Zero with a Whitespace:

# "/ 0" Where the space may be a tab etc.

// * 0 # a Division by Zero with Possibly Some

# spaces: "/ 0" or "/ 0" or "/ 0" ETC.

/// s * 0 # a Division by Zero with Possibly Some

# whitespace.

///s*0/.0* # as the prepvious one, but with decimal

# Point and maybe some 0s after it. Accepts

# "/ 0." And "/0.0" and "/0.00" etc AND

# "/ 0." AND "/ 0.0" and "/ 0.00" ETC.

Replacement and translation

Perl can be replaced on a match basis. This function can be implemented with the S function. If you do not use a matching operator, the replacement is considered to operate the $ _ variable.

Replace London with London in String $ Sentence Use the following expression:

$ SENTENCE = ~ S / London / London /

Use $ _ variables to do this:

S / London / London /

The result of the expression is the number of times the occurrence, so or 0 or 1.

Option

The above example only replaces the first matching string, and the G parameter can be replaced:

S / London / London / G

The result of returning is 0 or the number of times is replaced.

If we want to replace London, London, London, etc., you can do this:

S / [ll] [oo] [nn] [dd] [oo] [nn] / london / g

But you can have a simpler way - use I option (ignore the case):

S / London / London / GI

Memory method

If you remember the matching method, it can be more convenient in the future. Any matching that occurs in parentheses is recorded in the variable $ 1, ..., $ 9. These characters used in the same RE can be represented by /1, and.../9:

$ _ = "Lord Whopper Of Fibbing";

S / ([A-Z]) /: / 1: / g;

Print "$ _ / N";

This code replaces any uppercase letters in the form surrounded by the colon. The result is: l: ORD: W: Hopper of: f: ibbing. Variable $ 1, ..., $ 9 is read-only variable, you can't modify them.

Another example, judging the statement:

IF (/(/b. /b) / 1 /)

{

Print "Found $ 1 repeated / N";

Any repeated words will be judged. Each / b represents a word boundary,. Matches any non-empty string, so /b. /b matches the contents of any two word boundaries. Then be remembered, stored in / 1, and the remainder of the program is used.

The following expression exchanges the first and last character of the _ variable:

s /^(.) (.) (.) $/ 3/2/1 /

^ And the beginning and end of matching line. / 1 Store the first character, / 2 stores a portion other than the first and last characters, and the last character is stored in / 3. Then / 1 and / 3 interchange.

After matching, you can use a special read-only variable $ ~, $ & and $ 'to find the contents before, among the contents. There is

$ _ = "Lord Whopper Of Fibbing";

/ pp /;

After that, the following expression is true (EQ represents a string matching judgment).

$ `EQ" Lord WO ";

$ & EQ "PP";

$ 'EQ "Er Of Fibbing";

The variables can be used in replacement expressions, so

$ Search = "The";

S / $ search / xxx / g;

Any occurrence of any appearance will be replaced with xxx. If you want to replace There, you can't use S / $ Searchre / XXX / because the program will use it as a variable $ Searchre. Can be used to replace theRe with curly brackets:

$ Search = "The";

S / $ {search} RE / XXX /;

translation

TR function realizes the translation of characters to characters. The following expression replaces the variable $ SENTENCE E, B is D, C is F. Expression returns the number of times.

$ SENTENCE = ~ TR / ABC / EDF /

Most special RE codes cannot be used in the TR function. For example, the following statement calculates the number of aster numbers in the SENTENCE variable and then stores in variable $ count.

$ count = ($ SENTENCE = ~ TR / * / * /);

But "-" still represents "between". The following statement puts the variable $ _ convert to uppercase form:

Tr / a-z / a-z /; perl primary tutorial [Day 2] << >> Perl primary tutorial [Day 4]

转载请注明原文地址:https://www.9cbs.com/read-69901.html

New Post(0)