【问题标题】:How do I add an array ref to the middle of an existing array in Perl?如何在 Perl 中将数组 ref 添加到现有数组的中间?
【发布时间】:2010-11-04 18:00:52
【问题描述】:

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

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")

我似乎无法做到这一点。我查遍了谷歌,一无所获。

【问题讨论】:

  • 第二个不是数组。它是一个数组引用。
  • 查看 perldoc perlreftut 以获得关于 Perl 参考的优秀简短教程。

标签: arrays perl multidimensional-array


【解决方案1】:

所以你使用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" );

如果您不想要嵌套数组。

【讨论】:

  • 我认为他的意思是也插入到现有数组中。不过还不是很清楚。
【解决方案2】:

要记住的重要一点是 () 和 [] 之间的区别。 '()' 给你一个元素列表,例如。 (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.

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

【讨论】:

    【解决方案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;
    

    【讨论】:

      【解决方案4】:

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

      @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)'
      

      【讨论】:

        【解决方案5】:

        你拥有的是一个数组和一个数组引用。

        #!/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";
        }
        

        【讨论】:

          【解决方案6】:

          使用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'
                  ];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-01-10
            • 2021-09-30
            • 2011-04-21
            • 1970-01-01
            • 2017-06-13
            • 2016-05-03
            • 2022-06-15
            相关资源
            最近更新 更多