Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-26 09:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found