more useful options | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
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: 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? In reply to N-Dimensional Arrays on the fly by qumsieh
|
|