Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to create HASH with multiple values per Key

by gasho (Beadle)
on Apr 17, 2006 at 14:31 UTC ( [id://543824]=perlquestion: print w/replies, xml ) Need Help??

gasho has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I have two arrays that I want to combine into HASH. Problem is there are multiple values per key. How can I combine the two arrays into HASH? Thanks a lot
@Object = qw(FolderObject); @Property = qw(FolderProperty1 FolderProperty2 FolderProperty3); %O_P #Desired Print output #FolderObject ==> FolderProperty1 FolderProperty2 FolderProperty3

Replies are listed 'Best First'.
Re: How to create HASH with multiple values per Key
by ptum (Priest) on Apr 17, 2006 at 14:51 UTC

    As mentioned by Transient, you would take a reference to the second array and use it as the value of the hash, keyed by the values of the first hash:

    use strict; use warnings; my @Property = qw(FolderProperty1 FolderProperty2 FolderProperty3); my %O_P = ( 'FolderObject' => \@Property );

    Then whenever you wanted to refer to the properties, you would have to dereference, like this:

    my $second_property = $O_P{'FolderObject'}->[1];

    If @Object and @Property are prepopulated with a large number of values, you need to figure out how to take the right slices from @Property to assign the right values to each @Object (maybe they all have the same number of properties?).

    As mentioned, this assumes that the keys in @Object are unique -- you'll overwrite part of your hash if there are duplicate entries in @Object.

    Update: Ah, I see you are parsing XML. As mentioned by davidrw, you may not need to re-invent the wheel, here. :)


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: How to create HASH with multiple values per Key
by Transient (Hermit) on Apr 17, 2006 at 14:35 UTC
    You just need to make the hash value a reference to the Property array. The only question I would have in the matter is what if @Object had multiple values?
      I am looping through a file that contains both Objects and Properties and storing Objects in @Objects and Properties of particular Object in @Properties e.g of the file
      <AllObjectsProperties> <Object name="O1"/> <Properties> <Prop name="P1"/> <Prop name="P2"/> <Prop name="P3"/> </Properties> <Object name="O2"/> <Properties> <Prop name="S1"/> <Prop name="S2"/> <Prop name="S3"/> </Properties> </AllObjectsProperties>
        XML::Simple or one of the other XML modules may suit your needs and save you the parsing/looping/data structure creation.
Re: How to create HASH with multiple values per Key
by gasho (Beadle) on Apr 17, 2006 at 15:57 UTC
    Thanks you all to pointing to use XML::Simple; It saved my day :) Great and on time Help when you need it :)
Re: How to create HASH with multiple values per Key
by izut (Chaplain) on Apr 18, 2006 at 13:30 UTC
    Someone said in #perl at irc.freenode.org that XML::Simple sometimes can't do what you expect (I think it was merlyn), and it can be a memory hog. You can try XML::Smart and see if it fullfil your needs.

    Igor 'izut' Sutton
    your code, your rules.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://543824]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-18 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found