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:
This results in $str holding: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 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).for my $a (0 .. 4 - 1) { for my $b (0 .. 5 - 1) { for my $c (0 .. 6 - 1) { $array[$ a][$ b][$ c] = 0; } } }
I could only think of the same approach: Create a sub on the fly that can be called with the proper arguments. Something like:
Now $sub looks like this:my $i = 0; my $sub = 'sub { $array' . join '' => map {'[$_[' . $i++ . ']]'} 1 .. $dim; $sub .= ' = $_[-1] }' . "\n";
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.sub { $array[$_[0]][$_[1]][$_[2]] = $_[-1] }
Is there a better way?
Thanks,
--Ala
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: N-Dimensional Arrays on the fly
by BrowserUk (Patriarch) on Aug 20, 2004 at 01:41 UTC | |
by qumsieh (Scribe) on Aug 20, 2004 at 05:21 UTC | |
by ikegami (Patriarch) on Aug 20, 2004 at 15:25 UTC | |
Re: N-Dimensional Arrays on the fly
by blokhead (Monsignor) on Aug 20, 2004 at 00:28 UTC | |
Re: N-Dimensional Arrays on the fly
by Zaxo (Archbishop) on Aug 20, 2004 at 01:45 UTC | |
by qumsieh (Scribe) on Aug 20, 2004 at 05:19 UTC | |
Re: N-Dimensional Arrays on the fly
by Prior Nacre V (Hermit) on Aug 20, 2004 at 20:50 UTC |
Back to
Seekers of Perl Wisdom