Oh yee of little sanity, check ye pod before you falleth from grace.. ;-^)

Check out perlref or perlreftut for help on creating references and 2d arrays. And there's always the Categorized Questions and Answers section.

to make a two dimensional array (an array of arrays), do something like:

my @array = (1,2,3); my @2darray = (\@array, \@array, \@array); #or my @3darray = ( [ [11,12,13], [21,22,23], [31,32,33] ] ); print $3darray[0][3][3]; # yields 33
update: Actually print $3darray[0][2][2]; # yields 33. Just keepin' you on yer toes, zort Masem.

update:
Disregard what Zaxo said.

You don't lose the anonymous hash since it is being returned implicitly (because it is the last statement in sub, and bless returns the reference being blessed).

However, I do like to return things explicitly, as you never know what'll happen in perl 6 (doesn't really matter if you don't use it, but its good to be as explicit as possible).

Basically, what I would do is:

package foo; sub new { my ($class) = shift; my $self = {}; # a ref to an anon hash $self->{some_scalar} = "blah"; $self->{an_array_of_arrays} = [ [01,02], [11,12], [21,22]]; bless ( $self , $class ); return $self; } #so when you do my $new_object = new foo; my $array_ref = $new_object->{an_array_of_arrays}; print $array_ref->[1][1];

 
___crazyinsomniac_______________________________________
Disclaimer: Don't blame. It came from inside the void

perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"


In reply to (crazyinsomniac) Re: Complex Data Structures in Object Creation by crazyinsomniac
in thread Complex Data Structures in Object Creation by Anonymous Monk

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.