Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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}});
but now this looks so cumbersome. And what if I have matrices of different dimensions? Now I need matrixGet2D and matrixGet3D and matrixGet4D, etc. And all I really wanted to do was access a certain element using a set of coordinates stored somewhere else for a (relatively) tiny little script I'm writing to manage some data to assist me in a minigame within a game I'm playing. It's already growing to double or triple the size I thought it would originally. But I digress. Is there a simple inline method for using an arrayref as the indexes of a matrix?

And if there's any other blatantly unnecessary step in my code please let me know. It's been a while since I used Perl and mucking about in references was never my strong suit.

Thank you.

$,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q.    /_/__.,q..
Happy, sober, smart: pick two.

In reply to Accessing a matrix value from a dictionary of named coordinate tuples by xyzzy

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (2)
    As of 2021-05-08 21:50 GMT
    Find Nodes?
      Voting Booth?
      Perl 7 will be out ...

      Results (97 votes). Check out past polls.