Learning notes for Perl-Chapter 2

Source: Internet
Author: User

Chapter 2 scalar data

The scalar expression in Perl has a large range, including numbers and characters/strings. And numbers are not similar to floating point numbers in C, single precision, double precision, integer..., as long as the number is a scalar. In addition, the internal formats of all numbers are the same. Perl saves integers as floating-point numbers and calculates them based on floating-point numbers.

Numeric representation:(We can see the randomness of Perl in this regard)
1.25
255.000
7.25e45
-6.5e24
-12e-24
-1.2e-23
0
-40
61298040283768
61_298_040_283_768 _. Because commas have already played a more important role)
0377 (377 of octal)
0xff (hexadecimal ff)

Numeric Operators:
2 + 3 (2 plus 3 = 5, you can also add a space around the plus sign. Perl is loose in this regard, which is equivalent to the following)
5.1-2.4 (5.1 minus 2.4 = 2.7)
3*12
14/2
10.2/0.3
10/3
10% 3 (take the remainder, it should be noted that if the two values are decimal, the Perl processing method first converts decimals into integers, for example, 10.5% 3.2 10% is calculated according to 3)
2 × 3 (3 power of 2)

Single quotation mark string:A single quotation mark is not a part of a string and is used by Perl to determine the start and end of a string. It is the actual meaning of the character, unlike the double quotation mark string which has many escape characters. However, how to add a backslash to the front of a single quotation mark or a backslash.
'Fred'
''
'Hello/N' (here/N is not a carriage return line break, only 2 characters)
'/' // '(A single quotation mark and a backslash)

Double quotation mark string:Many escape characters can be interpreted (see the table below), and variables can be interpolated in double quotation marks.
"Barney" (same as 'Barney)
"Hello World/N"

Writing Method Description
/N Line feed
/R Enter
/T Tab
/F Form feed
/B Escape Character
/ Ring tones
/E ESC
/007 Octal value of any ASCII code (007 = bell)
/X7f Hexadecimal value of any ASCII code (here 7f = delete)
/CC Any ctrl key combination character (CTRL-C)
// Backslash
/" Double quotation marks
/L Lower case letters
/L Lowercase letters after all until/E
/U Uppercase letters
/U Uppercase letters after all until/E
/Q Add a backslash to reference a non-word character following it until/E
/E End/L,/U or/Q

String OPERATOR:"." And "X"

The. Operator is used for string connection.

"Hello". "world" (combined with "helloworld ")

"Hello". ''." world "(the combination is" Hello World ")

The X operator is called the string repetition operator, and the following digits represent the number of repetitions.

"Fred" x 3 (fredfred)

5x4 (5555, note that only integers can be used for repeated string operations. If the value is smaller than 1, it is null)

Automatic conversion of numbers and strings:

In most cases, Perl automatically determines whether the object is a string or a number based on the operator. In this regard, Perl processing is very intelligent. You can enable the warning switch of Perl, so that if you use it improperly, Perl will prompt you. That is, the-W switch I mentioned in the first chapter is used in the first line of the Perl program.

 

Scalar variable:

Perl variables start with the dollar sign $, start with a letter or underline, and are case sensitive!

Binary value assignment operator:

$ Fred + = 5; (equivalent to $ Fred = $ Fred + 5)

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

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

Priority:

Perl has the same priority as C ^_^

Comparison operator:

To list a table, make it clear:

Comparison Value String
Equal = EQ
Not equal ! = Ne
Less < Lt
Greater > GT
Less than or equal <= Le
Greater than or equal > = Ge

Two tips:

  1. The method is the opposite of the method used by shell.
  2. How to remember the keywords of a string: in fact, they are all written in English words. It's okay to understand what it means. The details are as follows (not necessarily accurate ^ _ ^, so it is easy to remember ): eq = equal, Ne = not equal, Lt = less than, GT = great than, Le = less than, GE = great

If control structure:

An example is provided 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 without the else segment. Add else to the above:

If ($ name GT 'fred '){

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

} Else {

Print "'$ name' does not come after 'fred'./N ";

Print "maybe it's the same string, in fact./N ";

}

I have a question here. The two examples are all copied from the book. According to the preceding results, $ name should not be enclosed in single quotes in the print statement, so that the variable content can be truly displayed. If a single quotation mark is added, does it mean $ name? Test now: I added $ name = "Marco" before this example ";

However, the result is displayed as follows:

Bash-2.05 $./test. PLX
'Marco' comes after 'fred 'in sorted order.

It indicates that single quotes can still explain the variable content. However, I continue to verify whether single quotes can really interpret variables, so I wrote a few simple words:

#! /Usr/local/bin/perl-W
$ Named = "Marco ";
Print '$ named ';

The error message is displayed as follows:

Bash-2.05 $./test. PLX
Name "Main: named" used only once: Possible typo at./test. PLX Line 2.

If you add double quotation marks on both sides of the single quotation mark, it will be OK. The output result is as follows:

'Marco'

If single quotes are placed in double quotes, single quotes are single quotes. OK, understand ^_^

How can we determine whether a Boolean value is true or false?

If the scalar value is UNDEF, 0, '', or '0', it is false. All others are true.

Get user input:

There should be many methods, but now we will introduce the <stdin> operator first. For example:

$ Line = <stdin>;

If ($ line EQ "/N "){

Print "that was just a blank line! /N ";

} Else {

Print "that line of input was: $ line ";

}

It is worth noting that <stdin> ends with carriage return. But it also puts the carriage return linefeed into the variable. Therefore, Chomp is a very useful operator named ^_^.

Chomp OPERATOR:

It is really a very useful parameter. Almost every program is used. It is actually a function. It is used to remove a linefeed at the end. If there are two or more linefeeds, Chomp with the same number will be used.

The most common or cool method of use is:

Chomp ($ test = <stdin> );

The usage is as follows:

$ Test = <stdin>;

Chomp ($ test );

The first type is recommended, which looks like a Perl programmer ^_^.

While control structure:

Example:

$ COUNT = 0;

While ($ count <10 ){

$ Count + = 1;

Print "count is now $ count/N ";

}

I don't need to explain it more. I believe it is a program that everyone on Earth can understand.

UNDEF value:

The value of a variable before the first value assignment is UNDEF. If UNDEF is used as a number, its function is equivalent to 0. Therefore, it can easily generate a value accumulators starting with NULL:

# Add some odd values:

$ N = 1;

While ($ n <10 ){

$ Sum + = $ N;

$ N + = 2;

}

Print "the total was $ sum./N ";

In this program, $ sum is not specified at the beginning, so it starts with UNDEF. Because it is an addition of numbers, it is used as 0.

When this variable is used as a character, it is similar to a null character.

Defined Function:

Is a function used to check whether the variable is UNDEF. For example:

$ Madonna = <stdin>;

If (defined ($ Madonna )){

Print "the input was $ Madonna ";

} Else {

Print "no input available! /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.