views:

868

answers:

6

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.

+1  A: 

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)'
Charlie Somerville
I agree, use references.
Ape-inago
+6  A: 

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.

ysth
I think he meant insert into an existing array too. Not totally clear though.
Nic Gibson
+1  A: 

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";
}
Alan Haggai Alavi
+3  A: 

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.

muteW
+1  A: 

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

brian d foy
+1  A: 

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'
        ];
Leonardo Herrera