Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Array of array vs hash

by Marshall (Canon)
on May 16, 2012 at 11:28 UTC ( [id://970814]=note: print w/replies, xml ) Need Help??


in reply to Array of array vs hash

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://970814]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-19 05:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found