user2076234
user2076234

Reputation: 71

Insert Array of Array perl

I have an array of array

@data = [["Hi", "Hello"],["Apple", "Orange"]];

And I need to insert a new array

@a = ["a", "b"];

I would like that the array @data looks like this

@data = [["Hi", "Hello"],["Apple", "Orange"], ["a", "b"]];

How can I do that?

Upvotes: 1

Views: 236

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 185790

When you type

[ "foo", "bar", "base" ]

it's not a simple array, but a reference to an array:

my $ref = [ "foo", "bar", "base" ];
print $ref;

Displays by example:

ARRAY(0x1d79cb8)

A simple array is @array assigned to this simple list:

my @array = ( "foo", "bar", "base" )

Still using a reference:

use Data::Dumper;

# Using $array_ref to store the reference.
# There's no reason to use an @array to store a
# memory address string...
$array_ref = [["Hi", "Hello"],["Apple", "Orange"]];

# Pushing in the dereferenced array ref
push @$array_ref,  ["a", "b"];

# Let's the doctor take a look in this body
print Dumper $array_ref;

Output:

$VAR1 = [
          [
            'Hi',
            'Hello'
          ],
          [
            'Apple',
            'Orange'
          ],
          [
            'a',
            'b'
          ]
        ];

Seems what you expect, no?


See perldoc perlreftut

Upvotes: 6

David W.
David W.

Reputation: 107090

You have:

@data = [                #Open Square Bracket
   ["Hi", "Hello"],
   ["Apple", "Orange"]
];                       #Close Square Bracket

And not this:

@data = (                #Open Parentheses   
   ["Hi", "Hello"],
   ["Apple", "Orange"]
);                       #Close Parentheses

The [...] syntax is used to define a reference to an array while (...) is defined as an array.

In the first one, we have an array @data with one member $data[0]. This member contains a reference to an array which is composed of two more array references.

In the second one, we have an array @data with two members, $data[0] and $data[1]. Each of these members contain one reference to another array.

Be very, very careful about that! I'm going to assume you meant the second one.

Let's use some syntactic sugar that can clean things up a bit. This is what you're representation looks like:

my @data;
$data[0] = ["Hi", "Hello"];
$data[1] = ["Apple", "Orange"];

Each entry in your array is a reference to another array. Since @data is merely an array, I can use push to push elements to the end of the array:

push @data, ["a", "b"];

Here I'm pushing another array reference. I could have done this too if it makes it easier to understand:

my @temp = ("a", "b");
push @data, \@temp;

I've created an array called @temp, and then pushed a reference to @temp onto @data.

You can use Data::Dumper to display your structure. This is a standard Perl module, so it should already be available on your installation:

use strict;
use warnings;
use feature qw(say);
use Data::Dumper;

my @data = (
    [ "Hi", "Hello"],
    [ "Apple", "Orange"],
);

push @data, ["a", "b"];
say Dumper \@data;

This will print out:

$VAR1 = [             # @data
      [           # $data[0]
    'Hi',
    'Hello'
      ],
      [           # $data[1]
    'Apple',
    'Orange'
      ],
      [           # $data[2]
    'a',
    'b'
      ]
    ];

Upvotes: 2

Related Questions