You still don't quite have the concepts. There is only a "2-D array" in Perl. See a post from today from Monk::Thomas with some awesome ASCII art depicting Perl arrays.
At the end you said: I return the array of array of references ...
Should be: "the array of array references" or "the array of arrayrefs" or "the array of references to arrays" ...
As for your latest code: (first I would recommend testing with a minimal script like the one I posted above. Then when you have it figured out, start plugging your own code/data back in)
(for each of two ...)
-
In your first sub you create an actual array eg:
@rot_mat
-
You populate the array with anonymous arrays containing your data eg:
$rot_mat[0][0] = 'foo';
-
You return a reference to the main array (to save memory) eg:
return \@rot_mat;
-
The second sub gets the arrayref by calling the first sub eg:
my $aref = &first_sub();
-
Second sub really wants the data in the second level of the arrayref. That data is stored in the series of anonymous arrays that compose the first level of the arrayref. So, to access the data we have to access the anonymous arrays, which we do through references to them. eg:
my $bar = $aref->[0]->[0];
# hard to read
-
Depending on what we are doing, we might copy the anonymous arrays into new arrayrefs inside the second sub (still just pointers to the original in-memory array) eg:
my $baz = $aref->[0];
my $quux = $aref->[1];
# now $baz is a reference to the anonymous array Perl created
# for the first element of @rot_mat when you assigned a value
# to $rot_mat[0][0] in the first sub
say 'yep' if $baz->[0] eq 'foo';
-
Or maybe just do a double loop to get at the data eg:
foreach my $anon_array ( @{ $aref } ) { # dereference the main array
foreach my $value ( @{ $anon_array } ) { # dereference the anon arra
+y
say 'yep' if $value eq 'foo';
}
}
-
TMTOWTDI
Hope this helps.
Remember: Ne dederis in spiritu molere illegitimi!
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.