Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Array of structures within an array of structures.

by VinsWorldcom (Prior)
on Sep 20, 2013 at 13:14 UTC ( [id://1055025]=note: print w/replies, xml ) Need Help??


in reply to Array of structures within an array of structures.

Something like this:

UPDATE: In my zest to copy the original code, I made a mistake in my code below. Thanks Laurent_R for pointing it out in the node below. Corrected code follows:

# your initial definitions here # maybe a loop starts here my $student; # single student my @students; # all students in 1 class $student = { studentName => 'John', studentSurname=>'Something', studentID=>'9534', age=>'12' }; push @students, $student; # Eliminate loading next student below explicitly # by using a loop to read from source file, DB, etc. $student = { studentName => 'Mary', studentSurname=>'Something2', studentID=>'5489', age=>'13' }; push @students, $student; %classroom = ( classroomID => 10, classroomName => 'classroom1', students => \@students ); # Rinse, repeat

Replies are listed 'Best First'.
Re^2: Array of structures within an array of structures.
by Laurent_R (Canon) on Sep 20, 2013 at 22:54 UTC

    Hi VinsWorldcom,

    I guess it's a typo, but this part in your code:

    $student = ( studentName => 'John', studentSurname=>'Something', studentID=>'9534', age=>'12' ); push @students, $student;

    probably does not work properly, as far as I can say. The value of $student is now 12 because you are assigning a list to a scalar, and you're then pushing 12 into @students.

    You probably want either directly an hash ref like this:
    $student = { studentName => 'John', studentSurname=>'Something', studentID=>'9534', age=>'12' }; push @students, $student;

    Or an hash on which you later take a ref:

    %student = ( studentName => 'John', studentSurname=>'Something', studentID=>'9534', age=>'12' ); push @students, \%student;
Re^2: Array of structures within an array of structures.
by VinsWorldcom (Prior) on Sep 20, 2013 at 23:37 UTC

    You're right, I'm wrong. I've corrected my code in the node above (so as to not leave incorrect code there for posterity) and provided credit to you so your above correction doesn't seem misplaced.

    Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1055025]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found