如何在Perl中的现有数组中间添加数组引用?

时间:2009-06-22 03:09:38

标签: arrays perl multidimensional-array

我希望能够将数组放入数组中。例如,我可能有这样的数组:

my @array1 = ("element 1","element 2","element 3");

然后我有另一个数组

my $array_ref = ["this will", "go between", "element 1 and 2"];

我想将$array_ref放入第一个数组中 看起来像这样:

("element 1",["this will", "go between", "element 1 and 2"],"element 2","element 3")

我似乎无法做到这一点。我看着谷歌,一无所获。

6 个答案:

答案 0 :(得分:7)

所以你用splice用你想要的元素替换以元素1开头的0个元素(第二个元素,第一个元素是元素0):

splice( @array, 1, 0, ["this will", "go between", "element 1 and 2"] );

或者你的意思是:

splice( @array, 1, 0, "this will", "go between", "element 1 and 2" );

如果你不想要嵌套数组。

答案 1 :(得分:4)

要记住的重点是()和[]之间的区别。 '()'为您提供元素列表,例如。 (1,2,3)然后您可以将其分配给数组变量 -

my @listOfElem = (1, 2, 3);

'[]'是array reference并返回一个标量值,您可以将其合并到列表中。

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.

如果你必须在阵列中间动态添加数组元素,那么只需使用其他帖子中详述的“拼接”。

答案 2 :(得分:3)

在完成Intermediate Perl的第一部分后,您将会理解这一点,其中包括参考和数据结构。您还可以查看Perl data structures cookbook

简而言之,您使用引用(只是标量)将数组存储在另一个数组中:

my @big_array = ( $foo, $bar, \@other_array, $baz );

在您的情况下,您使用了匿名数组构造函数,只想将其拼接成现有数组。这是一个数组引用没什么特别的:

splice @big_array, $offset, $length, @new_items;

在您的情况下,您想从元素1开始,删除0项,并添加您的引用:

splice @big_array, 1, 0, $array_ref;

答案 3 :(得分:1)

尝试使用临时数组,如下所示:

@temp_arr = ("this will", "go between", "element 1 and 3");
@my_arr = ("element 1", \@temp_arr, "element 3");

您可以使用以下子元素:

print $my_arr[1]->[0]; # prints 'this will'

参考这样的子阵列:

print @$my_arr[1]; # Right! Prints 'this willgo betweenelement 1 and 2'

# Don't do this:
print $my_arr[1]; # Wrong! Prints something like: 'ARRAY(0xDEADBEEF)'

答案 4 :(得分:1)

您拥有的是数组和数组引用。

#!/usr/bin/perl

use strict;
use warnings;

my @array    = ("element 1","element 2","element 3");
my $arrayref = ["this will", "go between", "element 1 and 2"];

splice( @array, 1, 0, $arrayref );  # Grow the array with the list (which is $arrayref)

for ( my $i = 0; $i <= $#array; $i++ ) {
    print "\@array[$i] = $array[$i]\n";
}

答案 5 :(得分:1)

使用splice

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @array1 = ("element 1", "element 2", "element 3");
my $array_ref = ["this will", "go between", "element 1 and 2"];
splice(@array1, 1, 0, $array_ref);
print Dumper \@array1;

这将打印以下内容:

$VAR1 = [
          'element 1',
          [
            'this will',
            'go between',
            'element 1 and 2'
          ],
          'element 2',
          'element 3'
        ];