In my code, 2D arrays are arrays of references to arrays (any other way to implement them?).
The answer to the question is "yes", but I wouldn't use any of the ways I can think of implementing 2D arrays other than having arrays of references to arrays.
When i pass a matrix to a subroutine, i pass the subroutine a reference to the matrix (also, any other way to pass such a 2D array?).
Yes, you could use a package variable for your array, and pass a glob. But that's very Perl4ish, and buys you little in Perl5; beside from that, people will have a harder time understanding your code. Passing a reference is the standard way.
And since efficiency is quite a concern to me, i am trying to know if there's a better, more efficient way to dereference a 2D array and modify the new array without affecting the original one.
It depends. If the sub you call is supposed to return a new, completely different array, the values have to be copied one way or another. I'd write it different though:
sub foo { my $array = shift; my $copy = [map {[@$_]} @$array]; ... Changes in $copy won't affect $array ... return $copy; }
You may be able to speed it up by doing the copying in XS (or use a module that does so), but it still scales lineary with the number of elements in $array.

However, if you only need to make changes in the array in the subroutine, and don't need the changes when the sub is done, you could use local when modifying the array. No initial copy is needed.

#!/usr/bin/perl use 5.010; use strict; use warnings; use YAML; sub foo { my $array = shift; local $array->[0][1] = "bar"; print Dump $array; } my @array = ([1, 2, 3], [4, 5, 6],); foo \@array; print Dump \@array; __END__ --- - - 1 - bar - 3 - - 4 - 5 - 6 --- - - 1 - 2 - 3 - - 4 - 5 - 6
As you can see, $array[0][1] has a different value in foo, but outside the sub, @array is unmodified.

In reply to Re: dereferencing a double array by JavaFan
in thread dereferencing a double array by perlrocks

Title:
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, details, 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, summary, 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.