Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
> 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

In reply to Re: structuring data: aka walk first, grok later by oko1
in thread structuring data: aka walk first, grok later by chexmix

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (8)
    As of 2018-12-11 14:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How many stories does it take before you've heard them all?







      Results (55 votes). Check out past polls.

      Notices?
      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!