http://qs321.pair.com?node_id=384495

qumsieh has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm writing up a class that needs to create a multi-dimensional array on the fly. The dimension is specified through an argument to the new() method.

Needless to say, I'm looking for an elegant and efficient implementation.

The only way I can think of is to create a string of Perl code on the fly, and eval() it. Something like this:

my $dim = 3; my @size = (4, 5, 6); my $var = 'a'; my $str = ''; for my $i (0 .. $dim - 1) { $str .= ' ' x $i . "for my \$$var (0 .. $size[$i] - 1) {\n"; $var++; } $var = 'a'; $str .= ' ' x $dim . '$array' . join '' => map {'[$ ' . $var++ . ']'} 1 .. $dim; $str .= " = 0;\n"; $str .= " " x ($dim - $_) . "}\n" for 1 .. $dim;
This results in $str holding:
for my $a (0 .. 4 - 1) { for my $b (0 .. 5 - 1) { for my $c (0 .. 6 - 1) { $array[$ a][$ b][$ c] = 0; } } }
This solution strikes me as rather elegant, but somewhat inefficient, but I can live with that since I need to eval this only once. Now, my main concern is how I would set individual elements (through a call to another method).

I could only think of the same approach: Create a sub on the fly that can be called with the proper arguments. Something like:

my $i = 0; my $sub = 'sub { $array' . join '' => map {'[$_[' . $i++ . ']]'} 1 .. $dim; $sub .= ' = $_[-1] }' . "\n";
Now $sub looks like this:
sub { $array[$_[0]][$_[1]][$_[2]] = $_[-1] }
which I can eval and then call anonymously with the coordinates and the desired value. But, I will need to call this sub a huge number of times and hence think it will be rather inefficient.

Is there a better way?
Thanks,
--Ala