2

I want to be able to place an array into an array. For example, I may have an array like this:

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

Then I have another array

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

I want to place $array_ref into the first so that the first array looks like this:

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

I can't seem to do this. I looked all over Google and found nothing.

2
  • The second one is not an array. It is an array reference. Commented Jun 22, 2009 at 4:26
  • 1
    Check perldoc perlreftut for an excellent, brief tutorial on references in Perl. Commented Jun 22, 2009 at 11:31

6 Answers 6

7

So you use splice to replace 0 elements beginning with element 1 (the second element, the first is element 0) with your desired elements:

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

Or possibly you mean:

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

if you don't want nested arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

I think he meant insert into an existing array too. Not totally clear though.
5

The important point to remember is the distinction between () and []. '()' gives you a list of elements, for eg. (1, 2, 3) which you could then assign to an array variable as so -

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

'[]' is an array reference and returns a scalar value which you could incorporate into your list.

my $refToElem = ['a', 'b', 'c'];

In your case, if you are initializing the first array then you could simply insert the second array elements like so,

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.

If you have to add the array elements on the fly in the middle of the array then just use 'splice' as detailed in the other posts.

Comments

3

This is the sort of thing you'll understand after going through the first part of Intermediate Perl, which covers references and data structures. You can also look in the Perl data structures cookbook.

In short, you store an array in another array by using a reference (which is just a scalar):

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

In your case, you used the anonymous array constructor and just want to splice it into an existing array. There's nothing special about it being an array reference:

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

In your case, you wanted to start at element 1, remove 0 items, and add your reference:

splice @big_array, 1, 0, $array_ref;

Comments

2

What you have is an array and an array reference.

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

Comments

2

Use 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;

This will print the following:

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

Comments

1

Try having a temporary array, like this:

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

You can use the sub-elements like this:

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

Refer to the subarray like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.