Bearing in mind (as mentioned above) that these should be arrays of hash-references and not hashes (see docs for perlref and perlreftut)...

It's a perfectly reasonable representation for the data, depending on how you want to handle the data. In my own work, I very frequently deal with arrays of hasherefs. Granted, though, it's not the only way, or necessarily the best. That's going to depend on what you want to do with it.

This can be a good way to represent very simple objects or rows of a database table. However, if you want to have a data structure which, in and of itself, enforces the homogeneity of the individual rows/objects, then you can go with an array of array-refs:

my @data = ( [ 'Mrs', 'Linda', 'Carolo', '201', '148', 'she@borg.org' ], [ 'Mrs', 'Jean', 'Andronlo', '317', '167', 'j@alo.com' ], # etc );
And, if you like, you can keep the column name => column index map in a hash:
my %index = ( title => 1, first => 2, last => 3, room => 4, phone => 5, email => 6, );
and then you can reference the items in a row:
foreach my $row (@data) { print "$row->[$column{title}] $row->[$column{first}] $row->[$column +{last}]\n"; }
There's also something in perl called a "psuedo hash" which is a means by which the language does what I showed above (using a name as an index in an array, rather than a number), but I'd avoid them if possible.

Anyway, that (above) is just one other example of how you might store a table... ultimately the "best" method for storing your data table will be dictated by what you intend to do with it. However, the array of hashrefs is about the simplest, most flexible way (though some people might gripe about the "wasteful"ness (both in space and time) of using a hash-lookup for each element of each row).


------------
:Wq
Not an editor command: Wq

In reply to Re: Is this a reasonable data structure? by etcshadow
in thread Is this a reasonable data structure? by Theo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.