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

xiaoxiao2021-03-06  108

Chapter II Scalar Data Perl The range represents a large range, 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 used here. Because the comma has a more important role) 0377 (octal 377) 0xFF (hexadecimal FF) numerical operator: 2 3 (2 plus 3 = 5, also You can add space in the left and right plus. 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 is necessary to point out, if 2 values ​​are decimal, then the process of Perl will first decimalize the integer, such as 10.5% 3.2 is calculated according to 10% 3) 2 ×× 3 (2 power) single quotes string: single quotes itself 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 break, just 2 characters) '/' // '(a single quotes, a backslash) Dual quotation marking string: you can explain a lot Side character (see table below), and in double 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 the non word character, until / E / E end / L, / u or / q string operator: " "And" x ". The operator is" Hello "used to connect to the string." World "(combined is" helloworld ")" Hello ". ''." World "(combined is" Hello World ") The X operator is called a string repetition operator, and the rear follows the number "Fred" X 3 (FredFredFred) 5 x 4 (5555, note, " And the automatic conversion of the string: In most cases, Perl automatically determines the object according to the operator is a string or a number. In this regard, the process of Perl is very intelligent, you can turn on the PERL warning switch so you use it Not suitable, 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 variable is starting with the US dollar sign $, with letters or underscores At the beginning, and write case! 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 is the same ^ _ ^ comparison operator: Or column table, look clear: Compare numeric string equal == EQ Equal! = NE smaller than GT less than or equal to <= LE is equal to or equal to> = GE two points:

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: 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. Below, on the top, ELSE: IF ($ Name GT 'Fred') {

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

} else {

Print "'$ name' does not come atTer '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 a diction: I added $ name = "marco" in front of this example; but the results show: bash-2.05 $ ./test.plx 'marco' comes after 'Fred' in sorted order. Description single quotes or The content of the variable can be explained. 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'; result Indeficit information: Bash-2.05 $ ./test.plx name "main :: name" used ") Used Only Once: Possible Typo at./test.plx line 2. If you add double quotes on both sides of single quotes, it is OK. The output is as follows: 'Marco' It turns out that if the single quotes are in the 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 are many ways, but now you will introduce this operator. The same, for example, 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 is ended in 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 way the mid-rulement medium is: $ test = ; chomp ($ test); recommended to use the first, this seems to Like a Perl programmer ^ _ ^. While Control Structure: Example: $ count = 0; While ($ count <10) {

$ count = 1;

Print "Count is now $ count / n";

} Needless to explain, I believe it is the understandable program of the earth people. UNDEF value: The variable is UNDEF before the first assignment. If 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 "Total WAS $ SUM. / N"; $ SUM in this program does not have a specified value, so it starts to be undef because it is a number of addition, so it is used as 0. When this variable is used as a character, its role is similar to the empty character. Defined function: It is a function specifically used to detect whether the variable is undef, example: $ madonna = ; if (Defined ($ Madonna)) {

Print "The Input WAS $ Madonna";

} else {

Print "No INPUT AVAILABLE! / N";

}

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

New Post(0)