0

I want to make an multidimensional array, but as i will declare it, i don't know how many element it will have, so I tried this:

my @multarray = [ ][ ];

Is it good?

2
  • 1
    Perl isn't C, and you don't need to initialise any sort of array. Just my @multarray is fine Commented Nov 30, 2014 at 16:40
  • Be sure to check the glossary's definition of autovivification--it's essential to understanding how Perl's multidimensional data structures operate. Commented Nov 30, 2014 at 18:37

2 Answers 2

3

Perl isn't C, and you don't need to initialise any sort of array. Just my @multarray is fine.

Observe

use strict;
use warnings;

my @data;

$data[2][2] = 99;
print $data[2][2], "\n";

The section in perldoc perlol on Declaration and Access of Arrays of Arrays will be of help here.

output

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

Comments

0

Perl doesn't support multidimensional arrays directly; an array is always just a sequence of scalars. But you can create references to arrays, and store the references in arrays, and that allows you to simulate multidimensional arrays.

The square bracket notation is useful here to create an array literal and return a reference to it. For example, the following creates an array of ('a','b',1234) and returns a reference to it:

my $ref_array = ['a','b',1234];

Here's an example of how to create (what we might call) a multidimensional array literal, of dimension 3x2:

my $multarray = [['a','b'],['c','d'],['e','f']];

So you could access the 'c' (for example) with:

print($multarray->[1]->[0]);

Now, you say you don't know what the final dimensions will be. That's fine; in Perl, you can conceptually view arrays as being infinite in size; all elements that haven't been assigned yet will be returned as undef, whether or not their index is less than or equal to the greatest index that has thus far been assigned. So here's what you can do:

my $multarray = [];

Now you can read and write directly to any element at any index and at any depth level:

$multarray->[23]->[19234]->[3] = 'a';
print($multarray->[23]->[19234]->[3]); ## prints 'a'

The syntax I've been using is the "explicit" syntax, where you explicitly create and dereference all the array references that are being manipulated. But for convenience, Perl allows you to omit the dereference tokens when you bracket-index an array reference, except when dereferencing the top-level array reference, which must always be explicitly dereferenced with the -> token:

$multarray->[23][19234][3] = 'a';
print($multarray->[23][19234][3]); ## prints 'a'

Finally, if you prefer to work with array variables (unlike me; I actually prefer to work with scalar references all the time), you can work with a top-level array variable instead of an array reference, and in that case you can escape having to use the dereference token entirely:

my @multarray;
$multarray[23][19234][3] = 'a';
print($multarray[23][19234][3]); ## prints 'a'

But even if you use that somewhat deceptively concise syntax, it's good to have an understanding of what's going on behind-the-scenes; it's still a nested structure of array references.

4 Comments

Perl supports multi-dimensional arrays just fine. Since a standard Intel CPU has no multi-dimensional array data type, every language emulates them in one way or another.
I guess the way I've been thinking about it is that Perl doesn't support any special built-in syntax or data type for multidimensional arrays, which contrasts with languages like C/C++ and Java where you can explicitly declare a multidimensional array type like int a[3][2] (in C/C++) or String[][] (in Java). But your point is well-taken.
I don't know why it's a common mantra that "technically, there are no multi-dimenesional arrays in Perl", but it seems to be a commonly-held belief
Sure, but the main difference in Perl is that it doesn't need to know the size of the array, which is where the OP was puzzled. In C you have to declare space for everything, and it's a real pain.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.