Perl list and Array variables _ Basic Tutorials

Source: Internet
Author: User
Tags array length arrays chop numeric value perl interpreter scalar stdin
One, List
A list is a sequence of values enclosed in parentheses that can be any value or NULL, such as: (1, 5.3, "Hello", 2), empty list: ().
Note: A list containing only one numeric value (for example: (43.2)) is different from the value itself (that is, 43.2), but they can be converted or assigned to each other.
List example:
($var, "a string")
(Num << 2)
($var 1 + $var 2)
($value, "The answer is $value")
Second, array--the storage of the list
The list is stored in an array variable, and unlike a simple variable, the array variable starts with the character "@", such as:
@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 differentiate between array variables and simple variables, the same name can be used for both array variables and simple variables, such as:
$var = 1;
@var = (one, 27.1, "a string");
But this is easy to confuse, so it is not recommended.
1, access to the array
The values in the array are accessed by subscript, and the first element is labeled 0. An attempt is made to access an array element that does not exist, but the result is null, but if an element that exceeds the size of the array is assigned, the array is automatically grown and the element value that was not originally null. Such as:
@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;
. Assigning values to lists with arrays
@list1 = (2, 3, 4);
@list2 = (1, @list1, 5); # @list2 = (1, 2, 3, 4, 5)
. Array assignment to a simple variable
(1) @array = (5, 7, 11);
($var 1, $var 2) = @array; # $var 1 = 5, $var 2 = 7, 11 ignored
(2) @array = (5, 7);
($var 1, $var 2, $var 3) = @array; # $var 1 = 5, $var 2 = 7, $var 3 = "" (NULL)
-Assign values to variables from standard input (STDIN)
$var = <STDIN>;
@array = <STDIN>; # ^d The symbol for the end input
2, square brackets and variable substitution in the string
"$var [0]" is the first element of the array @var.
' $var \[0] ' escapes the character ' [', which is equivalent to ' $var '. ' [0] ', $var replaced by a variable, [0] remains unchanged.
"${var}[0]" is also equivalent to "$var". [0] ".
"$\{var}" cancels the curly braces variable substitution function, contains 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)
-For real numbers
(2.1..5.3) = (2.1, 3.1, 4.1, 5.1)
(4.5..1.6) = ()
. for strings
("AAA" ...) AAD ") = (" AAA "," AaB "," AAC "," AAD ")
@day_of_month = ("01" ...) 31 ")
. can contain a variable or an expression
($var 1. $var 2+5)
. Tips:
$fred = "Fred";
Print ("Hello,". $fred. "!\n") x 2);
The results are:
Hello, fred!.
Hello, fred!.
4, the output of the array:
(1) @array = (1, 2, 3);
Print (@array, "\ n");
The results are:
123
(2) @array = (1, 2, 3);
Print ("@array \ n");
The results are:
1 2 3
5, List/array length
When an 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 the @array
($scalar) = @array; # $scalar = 1, that is, @array the value of the first element
Note: The length of the array can be programmed as follows:
$count = 1;
while ($count <= @array) {
Print ("element $count: $array [$count -1]\n");
$count + +;
}
6, Sub-array
@array = (1, 2, 3, 4, 5);
@subarray = @array [0,1]; # @subarray = (1, 2)
@subarray2 = @array [1..3]; # @subarray2 = (2,3,4)
@array [0,1] = ("string", 46); # @array = ("string", 46,3,4,5) now
@array [0..3] = (11, 22, 33, 44); # @array = (11,22,33,44,5) now
@array [1,2,3] = @array [3,2,4]; # @array = (11,44,33,5,5) now
@array [0..2] = @array [3,4]; # @array = (5,5, "", 5,5) now
You can exchange elements in the form of a sub array:
@array [1,2] = @array [2,1];
7, about the array of library functions
(1) sort--sorted by character order
@array = ("This", "is", "a", "test");
@array2 = sort (@array); # @array2 = (' A ', ' is ', ' test ', ' this ')
@array = (70, 100, 8);
@array = sort (@array); # @array = (8) Now
(2) reverse--inverted array
@array2 = reverse (@array);
@array2 = Reverse sort (@array);
(3) chop--array to tail
The meaning of the chop is to remove the last character, the line break, when the stdin (keyboard) input string is entered. And if it acts on an array, it does so with each element of the array.
@list = ("Rabbit", "12345", "quartz");
Chop (@list); # @list = ("Rabbi", "1234", "quart") now
(4) join/split--connection/Split
The first parameter of the join is the intermediate character used for the connection, and the remainder is an array of characters to be concatenated.
$string = Join ("", "This", "is", "a", "string"); # The result is ' this is a string '
@list = ("Words", "and");
$string = Join ("::", @list, "colons"); #结果为 "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.