http://www.perlmonks.org?node_id=1077910

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

Let's assume I have a matrix (any sort of n-dimensional array) @m. Better yet, let's pretend it's in an object, so to access it we have to do \$o->{m}. Now let's assume I have a dictionary (hash) that assigns names to certain elements in that matrix, which are represented by an anonymous array of coordinates:

```   %h = (
a => [3, 9, 4],
b => [2, 1, 3],
c => [9, 7, 2],
...
)
Is there any way to represent the element in the matrix using coordinates from the hash (let's also pretend that we're using a variable as the hash key) that is more elegant than \$o->{m}[\$h{\$key}->[0]][\$h{\$key}->[1]][\$h{\$key}->[2]]? All those brackets are giving me a headache.

The only alternative I could think of was:

```    my (\$x,\$y,\$z) = @{\$h{\$key}};
\$o->{m}[\$x][\$y][\$z] = ...
but making three new scalars to clarify a single operation (let's assume that there will only be a single lookup within this scope) seems a little excessive, and if you count the newline it uses the same exact amount of keystrokes. If this is something that I need to do in more than one place, then it would make sense to make something like:
```    sub matrixGet {
my (\$o, \$x, \$y, \$z) = @_;
return \$o->{m}[\$x][\$y][\$z];
}
sub matrixSet {
my (\$o, \$x, \$y, \$z, \$val) = @_;
\$o->{m}[\$x][\$y][\$z] = \$val;
}
...
\$o->matrixGet(@{\$h{\$key}});
\$o->matrixSet(@{\$h{\$key}},5);
or to be more flexible:
```    sub matrixGet {
my (\$m, \$x, \$y, \$z) = @_;
return \$m->[\$x][\$y][\$z];
}
...
matrixGet(\$o->{m},@{\$h{\$key}});