Hello-
I am working with matrices. Every matrix is implemented as a 2D array. In my code, 2D arrays are arrays of references to arrays (any other way to implement them?). 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?). The problem is that i want to dereference the reference. If i simply do:
modify(\@matrix);
sub modify{
$matrix_ref = shift @_;
@new_array = @$matrix_ref
}
This means that when i modify the @new_array, the changes will also be reflected to @matrix(right? because both @new_array and @matrix are arrays of the same set of references). To avoid this, i did the following
for(my $row=0; $row<=$#{$matrix_ref}; $row++){
for(my $col=0; $col<=$#{$matrix_ref->[$row]}; $col++){
push @new_array, $matrix_ref->[$row]->[$col];
}
}
basically copying the contents of @matrix to @new_array. But my code passes matrices several hundred times between my different subroutines and my matrices tend to be huge. 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. If i use my way, my code is very slow.
Thanks a lot for any tips
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.