One suggestion I would have is that, if your classrooms and students have unique IDs, you put them in a hash with that ID as the key, rather than an array. That will make accessing the data by those IDs simpler. Here's an example of creating such a structure, accessing its values in a couple of different ways, and adding to it:

#!/usr/bin/env perl use strict; use warnings; my %classroom = ( 10 => { classroomName => 'classroom1', student => { 9534 => { studentName => 'John', studentSurname => 'Doe', age => '12', } }, }, ); my $room = 10; my $kid = 9534; print <<END; Student $kid in room $classroom{$room}{classroomName} is $classroom{$room}{student}{$kid}{studentName} $classroom{$room}{studen +t}{$kid}{studentSurname}, who is age $classroom{$room}{student}{$kid}{age}. END # Add a classroom $classroom{11} = { classroomName => 'classroom2', }; # Add a student to new classroom $classroom{11}{student}{5489} = { studentName => 'Mary', studentSurname => 'Smith', age => 13, }; # Add a student to old classroom $classroom{10}{student}{3456} = { studentName => 'Jim', studentSurname => 'Baker', age => 14, }; # Print structure print "Students listed by classroom\n"; for my $cid (keys %classroom) { print "Classroom ID $cid, name $classroom{$cid}{classroomName}\n"; for my $sid (keys %{$classroom{$cid}{student}}) { my $student = $classroom{$cid}{student}{$sid}; #for less typin +g print ' ', join ' ', $sid, @{$student}{qw/studentName studentSurname age/}; print "\n"; } } # Print raw structure use Data::Dumper; print "\n\n", Dumper %classroom;

Aaron B.
Available for small or large Perl jobs; see my home node.


In reply to Re: Array of structures within an array of structures. by aaron_baugher
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.