SoFunction
Updated on 2025-04-07

Perl list and array variables detailed explanation

1. List
A list is a sequence of values ​​contained in brackets. It can be any numerical value or empty, such as: (1, 5.3, "hello", 2), empty list: ().
Note: A list containing only one numeric value (such as: (43.2)) is different from the value itself (ie: 43.2), but they can be converted or assigned 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
Lists are stored in array variables. Unlike simple variables, array variables are started with the character "@", such as:
    @array = (1, 2, 3);
Note:
(1) The initial value is an empty list when creating an array variable: ().
(2) Because PERL uses @ and $ to distinguish array variables from simple variables, the same name can be used for array variables and simple variables at the same time, such as:
    $var = 1;
    @var = (11, 27.1 , "a string");
But this is easy to confuse, so it is not recommended.
1. Access to arrays
The values ​​in the array are accessed through subscripts, and the first element is subscripted to 0. If you try to access an array element that does not exist, the result is NULL, but if you assign a value to an element that exceeds the size of the array, the array will automatically grow, and the original element value is NULL. like:
    @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; 
Use an array to assign values ​​to the list
    @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 a value to a variable from standard input (STDIN)
    $var = <STDIN>;
@array = <STDIN>; # ^D is the symbol of the end input
2. Square brackets and variable replacement in strings
"$var[0]" is the first element of the array @var.
"$var\[0]" Escape the 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 curly braces, including text: ${var}.
3. List range:
    (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
    (2.1..5.3) = (2.1, 3.1 ,4.1, 5.1)
    (4.5..1.6) = ()
Used for strings
    ("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. The output of the array:
(1) @array = (1, 2, 3);
    print (@array, "\n");
The result is:
    123
(2) @array = (1, 2, 3);
    print ("@array\n");
The result is:
    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 @array
($scalar) = @array; #$scalar = 1, that is, the value of the first element of @array
Note: The length of the array can be programmed as the number of loops 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[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
Elements can be exchanged in subarrays:
    @array[1,2] = @array[2,1];
7. Library functions related to arrays
(1)sort--Sort by character order
    @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--Invert the array
    @array2 = reverse(@array);
    @array2 = reverse sort (@array);
(3)chop--Array de-tail
The meaning of chop is to remove the last character - line break when entering a string by STDIN (keyboard). And if it works on the array, then every element in the array is handled like this.
    @list = ("rabbit", "12345","quartz");
    chop (@list); # @list = ("rabbi", "1234","quart") now
( 4) join/split--connect/split
The first parameter of join is the intermediate characters used for connection, and the rest is the array of characters 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