Get the App
SLTechnology News&Howtos  ›  Development  › 

How to store lists in Perl array

Shulou Source: shulou.com Published: 2022-06-02 07:43:48 09月19日 Update

This article is about how to store lists in Perl arrays. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Lists are stored in Perl array variables, and unlike simple variables, Perl array variables start with the character "@", such as: @ array= (1 ~ 2).

Perl array-storage of lists

Lists are stored in Perl array variables, which, unlike simple variables, start with the character "@", such as:

@ array= (1pm 2pm 3)

Note:

(1) when the Perl array variable is created, the initial value is an empty list: ().

(2) because PERL uses @ and $to distinguish between Perl array variables and simple variables, the same name can be used for both Perl array variables and simple variables, such as:

$var=1

@ var= (11pr 27.1, "astring")

But this is easy to confuse, so it is not recommended.

1. Access to Perl array

◆ accesses the values in the Perl array by subscript, with * elements subscribed to 0. If you try to access an element of the Perl array that does not exist, the result is NULL, but if you assign a value to an element that exceeds the size of the Perl array, the Perl array grows automatically, and the element value that did not exist is NULL. Such as:

@ array= (1, 2, 3, 4)

$scalar=$array [0]

$array [3] = 5witch now known arrayis (1meme 2pyrm 3pr 5)

$scalar=$array [4]; # now$scalar=null

$array [6] = 17 *

◆ Perl inter-array copy

@ result=@original

◆ uses the Perl array to assign values to the list

@ list1= (2, 3, 4)

@ list2= (1 recorder list1 penny 5); # @ list2= (1 recorder 2 minus 3 meme 4 rep 5)

Assignment of simple variables by ◆ Perl Array

(1) @ array= (5pm 7pm 11)

($var1,$var2) = @ array;#$var1=5,$var2=7,11 ignored

(2) @ array= (5pc7)

($var1,$var2,$var3) = @ array;#$var1=5,$var2=7,$var3= "" (null)

◆ assigns values to variables from standard input (STDIN)

$var=

@ array=;# ^ D is the symbol entered at the end

2. Square brackets and variable substitution in a string

"$var [0]" is the * * elements of the Perl array @ var.

"$var\ [0]" escapes the character "[" equivalent to "$var". "[0]", $var is replaced by a variable, and [0] remains the same.

"${var} [0]" is also equivalent to "$var". "[0]".

"$\ {var}" removes the variable substitution function of curly braces, including the text: ${var}.

3. List range:

(1) (1) (1) (1) (1) (1) 2) 3, 4, 5, 6, 7, 8, 9)

(2, 5... 7, 11) = (2, 5, 6, 7, 11)

(3. 3) = (3)

◆ is used for real numbers

(2.1... 5.3) = (2.1, 3.1, 4.1, 5.1)

(4.5.. 1.6) = ()

◆ is 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") x2)

The result is:

Hello,Fred!

Hello,Fred!

4. Output of Perl array:

(1) @ array= (1pm 2pm 3)

Print (@ array, "\ n")

The result is:

one hundred and twenty three

(2) @ array= (1pm 2pm 3)

Print ("@ array\ n")

The result is:

one hundred and twenty three

5. The length of the list / Perl array

When a Perl array variable appears where a simple variable is expected, the PERL interpreter takes its length.

@ array= (1pm 2pm 3)

$scalar=@array;#$scalar=3, which is the length of @ array

($scalar) = @ array;#$scalar=1, that is, the value of @ array***

Note: the length of the Perl array as the number of cycles can be programmed as follows:

$count=1

While ($count

Tags: Array variable character element result storage length string input difference subscript content more equivalence article access related good function function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Docker macOS Linux Shulou Tech Info