Perl Learning Basic Memo _perl

Source: Internet
Author: User
Tags array length arrays logical operators scalar table definition tmp file

1.Perl Array (1):

1 Initialize the array @array = ("Stringa", "StringB", "STRINGC");
(2) using negative index loop to retrieve print $array [-1]; #输出索引为 (-1 + 3)% 3 = 2 STRINGC
3 Dynamic growth $array [4] = "STRINGD"; #虽然 $array [3] has not yet been used, but in the use of dynamically assigned $array[4], has been left vacant for $array[3]
4 dynamic growth after the negative index print $array [-1]; #输出索引为 (-1 + 5)% 5 = 4 Stringd
5 print @array; #输出数组中所有值的无空格串接
6 print "@array"; #输出数组中所有值的空格分隔列表

2.Perl Array (2):

1 The array can hold different types of data (strings, values, arrays)
2) $size = @array; The statement gives the array length value of the array @array to the scalar $size, but if you use @array directly in some functions, you will not necessarily get the array length
3) $ #arrayname, the special variable holds the index value of the end of an array named Arrayname.
For example: @arr = (1,2,3,4,5);
Then $ #arr的值为4
$ #arr = 2; This truncates the array arr, the elements 4 and 5 are freed, and the array length values are changed
4) @array [ -1,4,7] outputs an element with an index value of -1,4,7, such that the call return value is not a scalar, but an array
5 multi-dimensional arrays are defined using multiple brackets
@D3array = (
[[1,2,3,4], [5,6,7], [8,9,0]],
[[' str1 ', ' str2 ', ' STR3 '], [345, 67, 8930]],
[[4,6,7], [2], ["Sud"]]
);

The array lengths of each dimension in multidimensional arrays do not need to be consistent, nor do internal data elements need to be consistent.
The way to get the array length of the first dimension is $size = @array;
The second dimension $size = @{$array [$i]};
Third dimensional $size = @{$array [$i] [$j]};
.......         ......
The point is, as long as the @ symbol is followed by an array variable, you can get its length, but when you exceed one dimension, the expression that gets the variable is wrapped in {}

3. Command line Execution Perl statement

1 perl-e ' command statement ' so you can execute Perl statements directly at the command line, but pay attention to the quotation mark matching rule.
2 perl-ne ' command statement ' filename This allows you to read the data in filename row by line, and then handle each row with a command statement.
For example a) Perl-ne ' print; ' Abc.txt
Output abc.txt file data on a line-by-row basis in a command-line interface
b) Perl-ne ' Print if/^192/'/etc/hosts > ~/hosts.tmp
Writes the contents of a host interpretation file in a Unix like system to the ~/hosts.tmp file, but only if the line that starts with 192 is written, because the statement performs a filter

3) ' OS command ' | Perl-ne ' command statement ' allows you to redirect the output of the previously executed OS command as an input stream to the Perl command that is executed later.
For example a) ls-al | Perl-ne ' Print; '
List all the subfolders and subdirectories in the current directory and enter them as input data into the Perl command, then Print

4.Perl Quote Rule

1 the contents of double quotes allow escape and variable resolution, abbreviated as qq/content
2 any character in single quotation marks will not be escaped and the variable will not be parsed, abbreviated as q/content
3 The contents of the inverted quotation marks if the OS command appears, the command is executed, and the resulting array replaces the command position, or as the assigned data, the abbreviation is qx/content/
4 the above abbreviation, the symbol to/content/can be replaced with other symbols, the effect is the same, such as QQ (content), qx! Content! , q+ content + et cetera, but the letters don't seem to

5.here Document Rules

1 The starting label does not use any quotes. The effect on the contents of the document equates to the use of double quotes.
2 The start tag uses single quotes. The effect on the contents of the document equates to the use of single quotes.
3 The starting tag uses an inverted quote. The effect on the contents of the document is equivalent to using an inverted quotation mark.

6. Variable initialization

Variables in Perl are allocated memory the first time they appear. If no explicit initialization is performed, it is assigned a value of 0 or an empty string, depending on the context in which the variable appears.
Using the defined function, defined $var can check whether the variable has been initialized.
Using the undef function, undef $var can release the contents of a variable.

7. Special variables

1) $_, the value of the variable is often used as the default parameter value, such as calling print; If no arguments are given, the $_ value is printed, and when the data is read using a file handle, it is read into the $_ if you do not specify what variable to use to save the read data.

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, but if you want to take a non string key value, it is best to insert it into the hash table as a single key/value pair assignment, rather than inserting it at initialization time.

3) on the hash table in 1, you can use the hash slice using the following operations:

Copy Code code 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 that, if the name is the same (except for the beginning% to @), is inserted in the original hash table, not created.

b) for the modified Ahash in 3, you can obtain a subset of its value collection in the following ways:

Copy Code code as follows:

@subSet = @aHash {' NewKey1 ', ' Key1 ', 456};

@subSet the order in which the elements are saved in an array, specifying the order of the keys when assigned.
Where @hash table name is used in this way, called hash slice.

9. Array hash Nesting

Copy code code as follows:

%ahash = (
"Key1" => "value1",
"Key2" => [
 &nb sp;       "str1", [1,2,3,4,5],
{
       & nbsp;      "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

The-> symbol in the above call can be omitted.

10. Operator context
1 when the context of the operator is numeric, the opening space in the operand is skipped, the first number in the operand is directly found, and the subsequent string is skipped. If the starting string for the operand is not a space or a number, the operand is resolved to 0. An exception is when the form of the operand is in the scientific notation format, which is interpreted as a whole.

Copy Code code as follows:

$str 1 = "5 594ASD";
$str 2 = "10";
$str 3 = "ASD 10";
$str 4 = "4e3 Asiddfi";
$sum = $str 1 + $str 2 + $str 3 + $str 4; # $sum values are 4015, 5 + 10 + 0 + 4000

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

11. Logical operator Parsing
Perl's various logical operators can be interpreted as short-circuiting, that is, once a valid result is obtained, the subsequent expression will not continue to parse, and the parsed value of the last parsed logical expression is returned, except for the XOR 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.