Explanation of Perl list and array Variables

Source: Internet
Author: User
Tags chop perl interpreter

I. List
The list is a sequence of values contained in parentheses. It can be any value or empty, for example: (1, 5.3, "hello", 2), empty list :().
Note: The list containing only one value (for example, (43.2) is different from the value itself (I .e., 43.2), but they can be converted or assigned values to each other.
List Example:
(17, $ var, "a string ")
(17, 26 <2)
(17, $ var1 + $ var2)
($ Value, "The answer is $ value ")
2. array-storage of lists
The list is stored in array variables. Unlike simple variables, array variables start with the character "@", such:
@ Array = (1, 2, 3 );
Note:
(1) When an array variable is created, the initial value is an empty list :().
(2) Because Perl uses @ and $ to distinguish between array variables and simple variables, the same name can be used for both array variables and simple variables, such:
$ Var = 1;
@ Var = (11, 27.1, "a string ");
But this is easy to confuse, so it is not recommended.
1. array access
. Access the values in the array through subscript. The first element subscript is 0. If you try to access an array element that does not exist, the result is null. However, if you assign a value to an element that exceeds the array size, the array will automatically grow and the element value that does not exist will be null. For example:
@ Array = (1, 2, 3, 4 );
$ Scalar = $ array [0];
$ Array [3] = 5; # Now @ array is (1, 2, 3, 5)
$ Scalar = $ array [4]; # Now $ scalar = NULL;
$ Array [6] = 17; # Now @ array is (1, 2, 3, 5, "", "", 17)
. Copy between Arrays
@ Result = @ original;
. Assign values to the list using arrays.
@ List1 = (2, 3, 4 );
@ List2 = (1, @ list1, 5); # @ list2 = (1, 2, 3, 4, 5)
. Array assignment to simple variables
(1) @ array = (5, 7, 11 );
($ Var1, $ var2) = @ array; # $ var1 = 5, $ var2 = 7, 11 ignored
(2) @ array = (5, 7 );
($ Var1, $ var2, $ var3) = @ array; # $ var1 = 5, $ var2 = 7, $ var3 = "" (null)
. Assign values to variables from standard input (stdin)
$ Var = <stdin>;
@ Array = <stdin>; # ^ d indicates the end input symbol.
2. square brackets and variable replacement in the string
"$ Var [0]" is the first element of array @ var.
"$ Var \ [0]" Escape Character "[", equivalent to "$ Var". "[0]", $ VaR is replaced by a variable, and [0] remains unchanged.
"$ {Var} [0]" is also equivalent to "$ Var". "[0]".
"$ \ {Var}" cancels the variable replacement function of braces, including the text: $ {var }.
3. List scope:
(1 .. 10) = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
(2, 5 .. 7, 11) = (2, 5, 6, 7, 11)
(3 .. 3) = (3)
. Used for real numbers
(5.3. 2.1) = (3.1, 4.1, 5.1)
(1.6) = ()
. Used as a string
("AAA"... "aad") = ("AAA", "AAB", "AAC", "aad ")
@ Day_of_month = ("01" .. "31 ")
. Can contain variables or expressions
($ Var1.. $ var2 + 5)
TIPS:
$ Fred = "Fred ";
Print ("hello,". $ Fred ."! \ N ") x 2 );
The result is:
Hello, Fred!
Hello, Fred!
4. array output:
(1) @ array = (1, 2, 3 );
Print (@ array, "\ n ");
Result:
123
(2) @ array = (1, 2, 3 );
Print ("@ array \ n ");
Result:
1 2 3
5. Length of the list/Array
When the array variable appears where the expected simple variable appears, the perl interpreter takes its length.
@ Array = (1, 2, 3 );
$ Scalar = @ array; # $ scalar = 3, that is, the length of @ Array
($ Scalar) = @ array; # $ scalar = 1, that is, the value of the first element of @ Array
Note: You can program the number of cycles based on the length of the array as follows:
$ COUNT = 1;
While ($ count <= @ array ){
Print ("element $ count: $ array [$ count-1] \ n ");
$ Count ++;
}
6. subarray
@ Array = (1, 2, 3, 4, 5 );
@ Subarray = @ array [0, 1]; # @ subarray = (1, 2)
@ Subarray2 = @ array [1 .. 3]; # @ subarray2 = (2, 3, 4)
@ Array [] = ("string", 46); # @ array = ("string",) now
@ Array [0 .. 3] = (11, 22, 33, 44); # @ array = (11,22, 33,44, 5) now
@ Array [, 3] = @ array [, 4]; # @ array = (, 5, 5) now
@ Array [0 .. 2] = @ array [3, 4]; # @ array = (5, 5, "", 5, 5) now
Elements can be exchanged in the form of subarrays:
@ Array [1, 2] = @ array [2, 1];
7. database functions related to Arrays
(1) sort -- sort by character
@ Array = ("this", "is", "A", "test ");
@ Array2 = sort (@ array); # @ array2 = ("A", "is", "test", "this ")
@ Array = (70,100, 8 );
@ Array = sort (@ array); # @ array = (100, 70, 8) now
(2) reverse -- reverse an array
@ Array2 = reverse (@ array );
@ Array2 = reverse sort (@ array );
(3) chop -- remove the array from the tail
The significance of chop is to remove the last character-line break when stdin (keyboard) is entered. If it is applied to an array, it will process every element in the array.
@ List = ("rabbit", "12345", "quartz ");
Chop (@ list); # @ list = ("Rabbi", "1234", "quart") now
(4) join/split -- join/split
The first parameter of join is the intermediate character used for the connection, and the rest is the character array to be connected.
$ String = join ("", "this", "is", "A", "string"); # The result is "this is a string"
@ List = ("Words", "and ");
$ String = join (":", @ list, "colons"); # The result is "Words: And: colons"
@ Array = Split (/: //, $ string); # @ array = ("Words", "and", "colons") now
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.