An array is an ordered list of scalar data.
Arrays can contain any number of elements. The smallest array can contain no elements, while the largest array can occupy all available memory.
An array literal is a series of values separated by commas inside parentheses. For example:
(1,2,3,4,5)#An array with five values 1,2,3,4,5
("zmd",1974,173.5)#With "zmd",1974,173.5 Array of three values
()#Empty array
($a,5)#Two values: the value of $a and 5
($a $b,6)#Two values
Array variables have separate arrays The value should start with @ instead of $. For example:
@zmd
Note that @zmd has no connection with $zmd. Perl maintains separate namespaces for different types of objects.
Array assignment is the same as scalar assignment and is also represented by the equal sign. Perl determines whether an assignment operation is a scalar assignment or an array assignment based on whether the assignment object is a scalar or an array variable.
If the array actual quantity only contains variable references (not expressions), the array actual quantity can also be used as a variable. It can be used on the left side of the assignment operator. For example:
($a,$b,$c)=(1,2,3)#Assign 1 to $a, 2 to $2, 3 to $3
If you assign a numeric variable to a scalar variable, then What is assigned to the scalar variable is the length of the array, such as:
@zmd=(1,2,3)#Assign (1,2,3) to @zmd
$a=@zmd#$a is 3, that is, @zmd The number of arrays
The access of array elements is similar to that in C language. The subscripts are arranged in sequential integers, and the numbering starts from 0.
($a,$b,$c)=(1,2,3)#Assign 1 to $a, 2 to $2, 3 to $3
If you assign a numeric variable to a scalar variable, assign it to The scalar variable is the length of the array, such as:
@zmd=(1,2,3)#Assign (1,2,3) to @zmd
$a=@zmd#$a is 3, which is the array of @zmd Number
Comprehensive example
@user1=("zmd","cxm");#Assign the two strings zmd and cxm to @user1
@user2=@user1;#At this time @user2=@user1= ("zmd","cxm")
@user3=("zk",@user1);#At this time@user3=("zk","zmd","cxm")
($one,@user4)= @user3;#At this time $one="zk"
@user1=();#Clear @user1
@int1=(1,2,3,4,5);$x=@int1;#Replace the array @ The number of int1 is assigned to the $x pure variable, $x=5
$x=$#int1;#$#This variable returns the value of the last number in the array (index)$x=4
($x)=@int1 ; #$x is equal to the first value of the array $x=1
$b=$int1[0]; #$b is equal to the first element value of the array $b=1
$c=@int1[0] ;#$c Same as above $c=1, so there are two ways to call the value in the array
$int1[0]=3;#Assign the value 3 to the first element of the array @int@int1=(3, 2,3,4,5)
$int1[0,1]=[7,8];#Assign 7 to the first element of the array and 8 to the second element of the array@int1=(7,8 ,3,4,5)
@int1[0,1]=@int1[1,0];#Exchange the first two elements of the array @int1(8,7,3,4,5)
($int1[ 0],$int1[1])=($int1[1],$int1[0]);#Same as above @int1=(8,7,3,4,5)
@int2=@int1[0,1 ];#int2=(8,7)
$int1[5]=6;#Assign 6 to the sixth element in the array @int1=(1,2,3,4,5,6)
The above is Perl variable (2)--array content. For more related articles, please pay attention to the PHP Chinese website (m.sbmmt.com)!