"Perl language entry" self-study notes - Chapter 2

zhaozj2021-02-16  51

Chapter II Scale Data

The scalar representation in Perl is wide, including numbers and characters / strings. Moreover, the numbers are not like floating point, single fine, double fine, integer ..., as long as it is a number is scalar. And all numbers have their internal formats. Perl saves integers as floating point numbers, and calculates according to floating point numbers.

Digital representation: (in this regard, it can be seen in this regard) 1.25255.0007.25E45-6.5E24-12E-24-1.2E-230-406129804028376861_298_040_283_768 (this number and the above representation method are the same, just reality It is a comma in life, and it is useful here. Because the comma has a more important role) 0377 (octal 377) 0xff (hexadecimal FF)

Numerical operators: 2 3 (2 plus 3 = 5, can also add spaces in the plus sign. URL is very relaxed, the following is equivalent) 5.1-2.4 (5.1 minus 2.4 = 2.7) 3 * 1214 / 210.2 / 0.310 / 310% 3 (more, it should be pointed out that if 2 values ​​are decimal, the process of Perl will first decoder into integers, such as 10.5% 3.2 is calculated according to 10% 3) 2 ×× 3 (2 3 power)

Single quotes string: single quotes itself is not part of the string, used to make Perl to determine the beginning and end of the string. It is the actual meaning of characters, unlike a double quotation string has a lot of escape. However, if you want to indicate that single quotes and backslashes are added to a backslash in front. 'Fred' '' 'Hello / N' (here / N is not a return line, just 2 characters) '/' // '(a single quotes, a backslash)

Dual quotation string: You can explain a lot of escape characters (see table below), and in dual quotes can be interpolated. "Barney" (the same as 'Barney') "Hello WORLD / N"

Writing meaning / N wrap / R carriage / T tab / F change page / B retracter / a ring / EESC / 007 arbitrary ASCII code eight-input value (here 007 = ring) / X7F arbitrary ASCII code Hexadecimal value (here 7F = delete) / CC arbitrary CTRL key combination character (here is Ctrl-C) // Reverse slope / "Double Quotes / L Next Letter lowercase / L All back letters lowering until / E / U next letter uppercase / u All back letter Upperline until / E / Q adds a backslash reference to non word characters, until / E / E end / L, / u or / q

String Operators: "." And "X"

. The operator is used to connect to a string

"Hello". "World" (combined is "HelloWorld")

"Hello". ''. "World" (combined is "Hello World")

The X operator is called a string repeating operator, and the number of times represented back with the number

"Fred" X 3 (FredFredFred)

5 x 4 (5555, note, only integers can be used for string repetition, if less than 1 is equal to empty)

Automatic conversion of numbers and strings:

In most cases, Perl automatically determines the object according to the operator is a string or a number. In this regard, Perl's processing is very intelligent, you can open Perl warning switch so that if you use it inappropriate, Perl will prompt you. That is, I said in the first chapter of the -w switch, use the first line of the Perl program. Scalar variable:

Perl's variable is starting with the US dollar sign, starting with letters or underline, and is case sensitive!

Binary assignment operator:

$ FRED = 5; (equal to $ FRED = $ FRED 5)

$ FRED * = 3; (equal to $ FRED = $ FRED * 3)

$ Str. = "" (equal to $ STR = $ Str. "" ")

priority:

Perl's priority and C are exactly the same ^ _ ^

Compare operator:

Or list a table, it looks clear:

The comparison value string is equal == EQ is not equal! = NE is less than GT less than or equal to <= LE greater than or equal to> = GE

Two tips:

The way she has the same way, and the average person is more accustomed to treatment here. How to memorize the keywords of the string: actually written by the English words, as long as it understands what it means. Specifically, as follows (not necessarily accurate ^ _ ^, convenient memory is good): EQ = Equal, ne = not equal, lt = less Than, gt = Great Than, le = Less Than, ge = Great Than

IF control structure:

By way of example to illustrate the structure of the IF statement:

IF ($ Name GT 'Fred') {

Print "'$ name' comes after 'fred' in sorted order./n";

}

This is the simplest IF statement, no ELSE segment. Then add ELSE on the basis of the above:

IF ($ Name GT 'Fred') {

Print "'$ name' comes after 'fred' in sorted order./n";

} else {

Print "'$ name' does not com instold 'fred' ./ n";

Print "Maybe It's The Same String, in Fact./N";

}

I have a question here. These two examples are moving down, according to the result of me, should be in the print statement $ name should not add a single quotation, so that the contents of the variable can be truly displayed. If you add a single quotes, don't you represent $ Name? I immediately tried to do it: I added $ name = "marco" in front of this example.

But the result is displayed:

BASH-2.05 $ ./test.plx 'marco' comes after 'Fred' in sorted Order.

Description Single quotes or can explain the contents of variables. However, I will continue to verify whether the single quotes can really explain the variables, so it is very simple to write a few words:

#! / usr / local / bin / perl -w $ named = "marco"; Print '$ named';

The result is indeed an error message:

BASH-2.05 $ ./test.plx name "main :: name" used "iplx line 2. If there is another double quotes in both sides of single quotes. The output is as follows:

'Marco'

It turns out that if the single quotes are placed in double quotes, single quotes are single quotes, without any other meaning. OK, understand ^ _ ^

How to determine the true and false of the Boolean value?

If the scalar value is undef, 0, '', or '0', it is fake. Others are true.

Get user input:

There should be many ways, but now introduced

This operator. Like, for example,

$ line =

;

IF ($ LINE EQ "/ N") {

Print "That Was Just A Blank Line! / N";

} else {

Print "That Line of Input WAS: $ LINE";

}

It is worth noting because

It is ended with the carriage return. But it will also put the return line in the variable. Therefore, you need a very useful operator of Chomp ^ _ ^.

CHOMP operator:

It's really a very useful parameter. Almost every program will be used. It is actually a function. Its role is to remove a newline of the end, if there are 2 or more of the same number of Chomp.

The most common or coolest way is:

CHOMP ($ TEST =

);

The mode of use of the midrangement is:

$ TEST =

;

CHOMP ($ TEST);

It is recommended to use the first one, which looks like a Perl programmer ^ _ ^.

While control structure:

example:

$ count = 0;

While ($ counn <10) {

$ count = 1;

Print "Count is now $ count / n";

}

You don't have to explain more, I believe it is the understandable process of the earth people.

Undef value:

The variable is UNDEF before the first assignment. If the undef is used as a digital, its function is equivalent to 0, so it can make it easy to generate a numerical accumulator with an empty start:

# 加 一些数:

$ n = 1;

While ($ n <10) {

$ SUM = $ N;

$ n = 2;

}

Print "The Total WAS $ SUM. / N";

There is no specified value in this program, so it starts to be undef because it is used as a 0.

When this variable is used as a character, its role is similar to the empty character.

Defined function:

Is it specifically used to detect whether the variable is a function of undef, examples:

$ Madonna =

;

IF (Defined ($ Madonna)) {

Print "The Input WAS $ Madonna";

} else {

Print "No INPUT AVAILABLE! / N";

}

Exercise: The topic can be completed within 5 minutes, it is not difficult. But still worth doing, especially new hands. I am a newbie, so I have made every title ^ _ ^

1. Write a program to calculate the circumference of the round of the radius of 12.5. The perimeter is 2Pi times of the radius (approximately 2 multiplied 3.141592654). The answer you get should be approximately 78.5.

2. Modify the program of the previous question, prompt the user input, and accept one half-diameter value entered by the user. This way users should be entered with 12.5, she should get the same answer as the previous question. 3. Modify the previous question, when the user enters a number of less than 0, the perimeter gets 0, not a negative number.

4. Write a program that prompts the user to enter and read two numbers (each occupied), then print out the results of the two multiplication.

5. Write a program that prompts the user to enter and read a string and a number (each occupied), then repeat the string on different rows, and the number of repetition is specified by the number.

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

New Post(0)