in reply to Array of structures within an array of structures.
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'
|
|---|