Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

The many ways of perl sometimes leave me struggling when facing what is considered complex data structures.

I have a list of items represented by a unique number. Entries for this item may be duplicated. So I want to create hash with a key of the items id number.

I then want the value to be an anonymous array that lists the entry id. So that my final data structure is a hash with a key for each unique item. The value will lead me to see the individual entries on that item.

Now comes coding it. I have decided that routine to build the hash should exist in a sub, and for future portabibility it should use the hash by reference. At this point my idea of notation gets tossed around.

So my question is based on the sketch above and the code below what is the best way to write this:

sub ReadSource{ my $DataSource = shift; my $HashRef = shift; foreach my $ItemID (@Data){ #Note @Data is not globally + fed into the sub I have skipped the Data code to focus on the proble +m. if (exists($$HashRef{$ItemID})){ push @{$$HashRef{$ItemID}}, $DataSource; }else{ $$HashRef{$ItemID} = [$DataSource]; } } }

I haven't tested the code as this structure just looks unpleasent. I used as reference (all puns intended as usual) Perl Objects, Reference & Modules Chapter 4 page 40 and the code on Autovivification. I am just wondering as I prefer indirect notation if there is a way to clean this up. I am going to test my code now but was throwing the question out to the perl community to see if I as usual am missing the bleeding obvious.

"No matter where you go, there you are." BB

In reply to Complex Data Structure Suggestions Wanted by Ninthwave

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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (8)
    As of 2020-12-01 21:18 GMT
    Find Nodes?
      Voting Booth?
      How often do you use taint mode?

      Results (20 votes). Check out past polls.