|Just another Perl shrine|
N-Dimensional Arrays on the flyby qumsieh (Scribe)
|on Aug 19, 2004 at 23:44 UTC||Need Help??|
qumsieh has asked for the wisdom of the Perl Monks concerning the following question:
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:
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:
Now $sub looks like this:
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?