要记住的重要一点是 () 和 [] 之间的区别。'()' 给你一个元素列表,例如。(1, 2, 3) 然后您可以将其分配给数组变量,如下所示 -
my @listOfElem = (1, 2, 3);
'[]' 是一个数组引用并返回一个标量值,您可以将其合并到您的列表中。
my $refToElem = ['a', 'b', 'c'];
在您的情况下,如果您正在初始化第一个数组,那么您可以像这样简单地插入第二个数组元素,
my @listOfElem = (1, 2, ['a', 'b', 'c'], 3);
#This gives you a list of "4" elements with the third
#one being an array reference
my @listOfElem = (1, 2, $refToELem, 3);
#Same as above, here we insert a reference scalar variable
my @secondListOfElem = ('a', 'b', 'c');
my @listOfElem = (1, 2, \@secondListOfElem, 3);
#Same as above, instead of using a scalar, we insert a reference
#to an existing array which, presumably, is what you want to do.
#To access the array within the array you would write -
$listOfElem[2]->[0] #Returns 'a'
@{listOfElem[2]}[0] #Same as above.
如果您必须在数组中间动态添加数组元素,那么只需使用其他帖子中详述的“拼接”。