Ok, I can imagine losing connection between an animal and its color as not significant for inventory purposes as long as every animal is accounted for. But how can losing a whole animal be insignificant? Just trying to understand the logic between your input and output data in this exercise. Let me make some assumptions that disambiguate, for me at least, the stated problem and suggest a data structure which could hopefully make task of populating it very easy without any information loss. Assuming that
  • owner's first and last name can somehow be considered a unique combination
  • and we do not want to use OO approach
  • and assuming we are forced to use current structure of @info array's elements
  • and @info array in your example means, e.g. as far as cats are concerned, that M.O. owns one white cat and B.T. owns one white cat and one brown cat
  • and we do not want to lose any data
  • and we want to allow any owner to have more than one animal of the same kind and of any color
  • and we want to be able to add or remove different kinds of data very easily (e.g. add day of birth or have multicolored animals)
  • and we want to be able to change output format very easily
  • then a reasonable structure for %pets will be, in my opinion, something like this:
    %pets = ( "Thompson" => { "Bill" => { dogs => [ { colors => ["black"], }, ], cats => [ { colors => ["brown"], }, { colors => ["white"], }, ], hamsters => [ { colors => ["black"] }, ], }, }, "Owens" => { "Mary" => { cats => [ { colors => ["white"], }, ], } }, );
    Loading @info into such %pets will not be difficult and %pets will allow you to produce a report in any format you want, so this is fairly flexible. I will not give code examples for these tasks due to time constraints and I should also say this hash structure is one of many approaches, but this one should work easily. It will allow you to extend the information in the future, e.g. add name, date of birth to each pet, height, weight, additional nicknames, additional colors, whatever you need. Hope this helps.

    In reply to Re^3: Struggling with complex data structures and doing useful operations on their elements and populating from arrays by hotpelmen
    in thread Struggling with complex data structures and doing useful operations on their elements and populating from arrays by hiyall

    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.