Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

2d arrays in Perl?

by jotti (Scribe)
on Aug 21, 2002 at 08:18 UTC ( [id://191670] : perlquestion . print w/replies, xml ) Need Help??

jotti has asked for the wisdom of the Perl Monks concerning the following question:

I'm not very familiar with Perl, yet. I would like to construct a 2d array which will store data about pupils in our school. We have a number of pupils and for each pupil I want to store Name, ClassID, a variable number of CourseCodes.

In C++ I would create a class, because Name and ClassID would be strings and CourseCodes would be a dynamic array of some kind. Should I go OO in Perl, too, or is it possible to do something like:
@thePupils[23]->{"ClassID"} = "99-A";
This would store classID 99-A to the 23:rd pupil in the array.
@courses = @thePupils[23]->{"CourseCodes"};
This should store the 23:rd pupil's all course codes in an array for further processing.
I've tried these lines, but I get different errors. How should I declare the whole array?


Replies are listed 'Best First'.
Re: 2d arrays in Perl?
by smitz (Chaplain) on Aug 21, 2002 at 08:24 UTC
    Soo close...

    Instead of
    @thePupils[23]->{"ClassID"} = "99-A";

    $thePupils[23]->{"ClassID"} = "99-A";

    I'm not claiming this to be the most efficent or elegant solution, but there you go...

Re: 2d arrays in Perl?
by physgreg (Scribe) on Aug 21, 2002 at 08:39 UTC
    If you want CourseCodes to be an array, you can add elements to it like this:
    push @{$thePupils[23]{"CourseCodes"}}, "99-A"; push @{$thePupils[23]{"CourseCodes"}}, "99-B";
    and then access them like this:
    print ${$thePupils[23]{"CourseCodes"}}[0]; print ${$thePupils[23]{"CourseCodes"}}[1];
      Instead of this..
      print ${$thePupils[23]{"CourseCodes"}}[0]; print ${$thePupils[23]{"CourseCodes"}}[1];
      Why not just do...
      print $thePupils[23]{"CourseCodes"}[0]; print $thePupils[23]{"CourseCodes"}[1];
Re: 2d arrays in Perl?
by perigeeV (Hermit) on Aug 21, 2002 at 09:28 UTC
    Only you know your data, but you may also consider a hash of hashes. That way you can use the student name as the hash key:

    $pupils{JohnDoe}->{"ClassID"} = "99-A";

    With an array anytime you want to manipulate a pupil/set of pupils' attributes you'll have to traverse the array looking for the proper name. With a hash keyed to their name you let Perl do it for you. Of course you had better be sure all the names are unique first.

Re: 2d arrays in Perl?
by Anonymous Monk on Aug 21, 2002 at 11:17 UTC
Re: 2d arrays in Perl?
by BUU (Prior) on Aug 21, 2002 at 12:14 UTC
    You could still go OO in perl, look at the OO tutorials to see more about it, as its far to complex for me to attempt to explain here. But if you want to call methods on each student and what not, you could declare a hash or array of students, and then make each student into an object. Of course, another semi-relevant thought was that you might want to try a database of some sort (possibly mySQL, or maybe postgreSQL, or even access) which would allow you a ton of flexibility in how you represented your students, as you could easily order by any field you want, select a certain number of reccords, and things of that nature. Of course, this might be overkill.