Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Array of array vs hash

by rakheek (Sexton)
on May 15, 2012 at 16:10 UTC ( #970671=perlquestion: print w/ replies, xml ) Need Help??
rakheek has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks, I am kinda a newbie level person. I was planning to use array of array. Just wanted to know if it is good practice. Thanks, Rakhee

Comment on Array of array vs hash
Re: Array of array vs hash
by toolic (Chancellor) on May 15, 2012 at 16:28 UTC
    It depends what you need to do with your data structure. Here are some resources:
Re: Array of array vs hash
by stevieb (Hermit) on May 15, 2012 at 17:57 UTC

    Arrays are ideal when you want to store lists which always retain their order, and/or you don't need access to a value through a name. Hashes are ideal when you don't care about the order, but you need to access the data through a known name (key).

    In this example, we need to keep the three arrays in order to keep a sequence which is always consistent, so we use an AoA:

    my @a = qw(1 2 3); my @b = qw(4 5 6); my @c = qw(7 8 9); my @all = ( \@a, \@b, \@c, ); for my $aref ( @all ){ print $_ for @{ $aref }; print "\n"; }

    Outputs:

    123 456 789

    A hash on the other hand means you want to be able to access the data by name, not by the order in which it is stored:

    my %hash = ( a => 1, b => 2, c => 3 ); print "$hash{ a }, $hash{ b }, $hash{ c }\n"; #output: 1, 2, 3

    A good example I've found is this... imagine you have a school with a bunch of classrooms that are full of students. If you just want to store a list of students names, put them into an array. However, to find the students within a class, you'll need to specify the name of the classroom to access it. An AoA wouldn't be very handy here, but a HoA sure would:

    # define the list of students per each class my @room1_pupils = qw( steve mike melissa ); my @room2_pupils = qw( meredith alexa chris ); # create the school, and assign the student lists to each # hash key my %school = ( room1 => \@room1_pupils, room2 => \@room2_pupils, ); # now you can get a list of students by room name (through # the reference) my @students = @{ $school{ room2 } };

    note: I did the assignments the long way because I don't know if OP knows about anonymous vars yet.

      Thanks! that was helpful.
Re: Array of array vs hash
by Marshall (Prior) on May 16, 2012 at 11:28 UTC
    I liked stevieb's post. Here is something else to consider:
    #!/usr/bin/perl -w use strict; ## An Array of Hash is the most similar Perl ## way of representing a 'C' array of structure my @classAoH = ( { name => 'Bob', grade => 'C', }, { name => 'Jane', grade => 'A', } ); ## An Array of Array can represent the same ## data, but we have to use an index number instead ## of a hash key to access the data my @classAoA = ( [ 'Bob' , 'C'], [ 'Jane', 'A'], ); foreach my $hash_reference (@classAoH) { print "$hash_reference->{name} $hash_reference->{grade} \n"; } foreach my $array_reference (@classAoA) { print "$array_reference->[0] $array_reference->[1]\n"; } __END__ Both foreach() loops print exactly same thing: Bob C Jane A The order is guaranteed because both are arrays of "something". The difference is that often {name} or {grade} means something more and is easier to understand in the rest of the code for the reader than [0] or [1]. So, "readability" is something to consider. Of course this is not "free". The AoH is less efficient than the AoA, but often that does not matter. Often the most important thing is understandability and maintainability and the AoH wins on that point.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970671]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2014-09-22 18:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls