Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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:

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


In reply to N-Dimensional Arrays on the fly by qumsieh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-16 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found