http://www.perlmonks.org?node_id=689337


in reply to structuring data: aka walk first, grok later

> What do I study so I can better see such things (e.g. how do I get there from here)? Are there general books 
on data structures, or will this knowledge just come with experience?

From my perspective, that's the most important question you've asked. It's not that hard - but it does take a little work. First, look at the data "per level" (as you already have) and consider the most appropriate structure for it - e.g., if it's a simple ordered list, where you don't really care about easy retrieval for specific, invidual items, then use a list(ref). Otherwise, use a hash(ref) - or, if you have a single item, terminate that branch with a scalar. E.g., if you have a list of employees with all their info, consider it this way:

1) Employees are identified by their unique company ID numbers - so the ID# can be a "pointer" to the rest of the data for a given employee. That sounds like a set of keys and values - i.e., a hash. So, we have:

%empl = ( 100000 => "data for employee 100000 will go here", 100001 => "data for employee 100001 will go here", ... );

2) The employee's name should be easily retrievable - in fact, all the parts of his name should be - so they need individual labels. So, we have a hashref:

%empl = ( '100000' => { first_name => 'Joe', last_name => 'Smith', ...

3) The employee might have an address that you'd want to retrieve line by line (say, so you could add individual formatting) - but you don't really care about a specific line all by itself. This argues for either a scalar, like 'Smith' above - or a listref, which would allow you to do that formatting. So:

%empl = ( '100000' => { first_name => 'Joe', last_name => 'Smith', address => [ '99 Main St.', 'Apartment 1209', 'Anytown, USA 999999' ], ...

4) If you felt that a structure needed a little elaboration in depth - i.e., you wanted to have an entry for all the phone numbers, but wanted to denote what kind of numbers they were - then the key ('phones') would become a pointer to another hashref:

%empl = ( '100000' => { first_name => 'Joe', last_name => 'Smith', address => [ '99 Main St.', 'Apartment 1209', 'Anytown, USA 999999' ], phones => { home => '111-111-1111' +, cell => '111-111-1112' +, fax => '111-111-1113' } ...

There aren't any guarantees, and things can get a little more complex than that - but this is a pretty good approach to designing or figuring out the data structure you need. Play with it for a while - then, go re-read the perldocs. I think you'll find that they make a lot more sense than they did before.


-- 
Human history becomes more and more a race between education and catastrophe. -- HG Wells