So, if I get you right, you want to have an array of classrooms, each classroom being a hash containing some information on the classroom and an array of students, each student being in turn a hash.

There are many ways to construct this structure, and the best way may vary depending on where your data is coming from. You might find it easier to start from the bottom (lower level structures) and make your way up.

You could start by creating a student list with something like this:

my @student_list; push @student_list, { studentName => 'John', studentSurname=>'Somethin +g', studentID=>'9534', age=>'12' }; push @student_list, { studentName => 'Mary' ,studentSurname=>'Somethin +g2', studentID=>'5489', age=>'13' };

The @student_list array is now an array of anonymous hash refs (references to student hashes). Next, you want to create a classroom hash, which could be done as follows:

my %classroom = (classroomID => 10, classroomName=>'classroom1', students => \@student_list);

The structure now looks like this:

0 HASH(0x80355e98) 'classroomID' => 10 'classroomName' => 'classroom1' 'students' => ARRAY(0x80359e18) 0 HASH(0x8006c050) 'age' => 12 'studentID' => 9534 'studentName' => 'John' 'studentSurname' => 'Something' 1 HASH(0x8032f958) 'age' => 13 'studentID' => 5489 'studentName' => 'Mary' 'studentSurname' => 'Something2'

The final step is to push a a ref to %classroom onto an array of classrooms:

push @classrooms, \%classroom;

To access the surname of the second student, one possible way:

print $classrooms[0]{students}[1]{'studentSurname'}; # prints 'Someth +ing2'

Or, if you prefer:

print $classrooms[0]->{students}->[1]->{'studentSurname'}; # prints 'Something2'

In reply to Re: Array of structures within an array of structures. by Laurent_R
in thread Array of structures within an array of structures. by atu

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.