Basic Perl learning Memorandum

Source: Internet
Author: User
Tags table definition tmp file

1. Perl array (1 ):

1) initialize the array @ array = ("stringA", "stringB", "stringC ");
2) print $ array [-1]; # output the stringC with the index (-1 + 3) % 3 = 2
3) dynamic growth $ array [4] = "stringD"; # Although $ array [3] is not used yet, it is used to dynamically assign a value to $ array [4, $ array [3] has been assigned vacant
4) print $ array [-1]; # output index (-1 + 5) % 5 = 4 stringD
5) print @ array; # concatenate all values in the output array without spaces
6) print "@ array"; # list of all values in the output array separated by Spaces

2. Perl array (2 ):

1) different types of data (string, value, array) can be saved in the array)
2) $ size = @ array; this statement returns the length of the array @ array to the scalar $ size. However, if @ array is directly used in some functions, the length of the array may not be obtained.
3) $ # arrayname: This special variable stores the end index value of the array named arrayname.
Example: @ arr = (1, 2, 3, 4, 5 );
Then the value of $ # arr is 4.
$ # Arr = 2; in this way, the array arr is truncated, its elements 4 and 5 are released, and the array length value is also changed.
4) @ array [-, 7] outputs the elements whose index values are-and 7 respectively. The returned values are not scalar but arrays.
5) multi-dimensional arrays are defined using multi-layered square brackets
@ D3array = (
[[,], [, 7], [, 0],
[['Str1', 'str2', 'str3'], [345, 67,893 0],
[[4, 6, 7], [2], ["sud"]
);

The length of each dimension array in a multi-dimensional array does not need to be consistent, and the internal data elements do not need to be consistent.
Obtain the length of the First-dimensional array by $ size = @ array;
2d $ size =@{$ array [$ I]};
3D $ size =@{$ array [$ I] [$ j]};
.............
The key is that when the @ symbol is followed by an array variable, the length of the array variable can be obtained. However, if the length exceeds one dimension, the expression used to obtain the array variable must be wrapped in {}.

3. Run the Perl Statement on the command line.

1) perl-e 'COMMAND statement' can execute Perl statements directly on the command line, but pay attention to the quotation mark matching rules.
2) the perl-ne 'COMMAND statement' filename allows you to read data in filename line by line, and then process each line with command statements.
For example, a) perl-ne 'print; 'abc.txt
Output Data in the abc.txt file line by line
B) perl-ne 'print if/^ 192/'/etc/hosts> ~ /Hosts. tmp
Write the host interpretation file in the UNIX like system one by one ~ In the/hosts. tmp file, only the rows starting with 192 are written, because the statement performs filtering.

3) 'OS command' | perl-ne 'COMMAND statement'. In this way, you can use the output of the previously executed OS command as the input stream and redirect it to the perl Command executed later.
For example, a) ls-al | perl-ne 'print ;'
List all sub-files and sub-directories in the current directory, input the sub-files to the perl Command as input data, and print them one by one

4. Perl quotation mark rules

1) content in double quotation marks can be converted into variable parsing, abbreviated as qq/content/
2) No character in a single quotation mark will be converted and the variable will not be parsed. The abbreviation is q/content/
3) if the OS command appears in the content in the quotation marks, the command will be executed, and the result array will replace the command location, or be used as the value-assigned Data, abbreviated as qx/content/
4) In the abbreviation above, the symbol pair/content/can be replaced with other symbols, with the same effect, such as qq (content) and qx! Content !, Q + content + and so on, but it seems that letters are not allowed

5. here document Rules

1) The start tag does not use any quotation marks. The effect on the document content is equivalent to using double quotation marks.
2) The start tag uses single quotes. The effect on the document content is equivalent to the use of single quotes.
3) The start tag uses backquotes. The effect on the content of the document is equivalent to the use of reverse quotation marks.

6. Variable Initialization

The variables in Perl are allocated memory when they appear for the first time. If no Explicit initialization is performed, the value is 0 or an empty string. The specific expression depends on the context of the variable.
Using the defined function, defined $ var can check whether the variable has been initialized.
With the undef function, undef $ var can release the variable content.

7. Special Variables

1) $ _. The value of this variable is often used as the default parameter value. For example, print is called in this way. If no parameter is given, the value of $ _ is printed; when you use a file handle to read data, if you do not specify the variables used to save the read data, it will also be read into $.

8. Hash
1) Hash table definition Syntax:
% AHash = (
'Key1' => "value1 ",
"Key2" => 'value2 ',
"Key3" => 123,
456 => "890"
);

2) the key values in the Hash table can be numbers, strings, arrays, or even another hash table. However, to use a non-string key value, it is better to insert a single key/value pair into the hash table, instead of inserting it during initialization.

3) for the hash table in 1), use the following operations to use hash slice:

Copy codeThe Code is as follows: a) @ aValues = qw (123 456 789 0 );
@ AHash {'newkey1', 'newkey2', 'newkey3', 'newkey4'} = @ aValues;

In this way, you can create a new hash table. If the name is the same (except for the starting % to @), it will be inserted in the original hash table instead of created.

B) to 3) The modified aHash can obtain the subset of its value set in the following way:

Copy codeThe Code is as follows: @ subSet = @ aHash {'newkey1', 'key1', 456 };

@ SubSet the sequence of elements stored in the array is the sequence of keys specified when values are assigned.
Here, the @ hash table name is called hash slice.

9. array HASH nesting

Copy codeThe Code is as follows: % aHash = (
"Key1" => "value1 ",
"Key2" => [
"Str1", [1, 2, 3, 4, 5],
{
"Key2.1" => "value2.1 ";
"Key2.2" => "value2.2 ";
}
],
"Key3" => {
"Key3.1" => "value3.1 ",
"Key3.2" => "value3.2 ",
},
);
Print "$ aHash {'key2'}-> [1] [3] \ n"; output 4
Print "$ aHash {'key2'}-> [2]-> {'key2. 1'} \ n"; Output value2.1
Print "$ aHash {'key3'}-> {'key3. 2'} \ n"; Output value3.2

In the preceding call, the-> symbol can be omitted.

10. Operator Context
1) when the context of the operator is a value, the space at the beginning of the operand will be skipped, and the first digit in the operand will be directly found, and the subsequent string will be skipped. If the start string of the operand is neither a space nor a number, the operand is parsed to 0. An exception is interpreted as a whole when the operand is in the scientific notation format.

Copy codeThe Code is as follows: $ str1 = "5 594asd ";
$ Str2 = "10 ";
$ Str3 = "asd 10 ";
$ Str4 = "4e3 asiddfi ";
$ Sum = $ str1 + $ str2 + $ str3 + $ str4; # $ sum is 4015, 5 + 10 + 0 + 4000

2) When the context of the operator is a string, all operands are parsed into strings.

11. parsing logical operators
All logical operators in Perl can be considered short-circuited, that is, once a valid result is obtained, the subsequent expressions are not parsed, And the parsing value of the last parsed logical expression is returned, except the exclusive or operator.

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.