Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How do I make a hash of arrays?

( #1977=categorized question: print w/ replies, xml ) Need Help??
Contributed by vroom on Jan 12, 2000 at 00:35 UTC
Q&A  > Data Structures


Answer: How do I make a hash of arrays?
contributed by chromatic

my %dramatis_personae = ( humans => [ 'hamnet', 'shakespeare', 'robyn', ], faeries => [ 'oberon', 'titania', 'puck', ], other => [ 'morpheus, lord of dreams' ], );
Access it like this:
foreach my $group (keys %dramatis_personae) { print "The members of $group are\n"; foreach (@{$dramatis_personae{$group}}) { print "\t$_\n"; } }
Answer: How do I make a hash of arrays?
contributed by Anonymous Monk

You can always...

push(@{$hash{$key}}, $insert_val);

...to load the arrays in the hash. That way you don't have to build the arrays, then dereference them into the hash.
Answer: How do I make a hash of arrays?
contributed by Anonymous Monk

my @arrayOne = 1..10; my @arrayTwo = 20..30; my %HashOfArrays = ( one => \@arrayOne ); $HashOfArrays{two} = \@arrayTwo; $HashOfArrays{IpointToTheSameArrayAsOne} = \@arrayOne ; $HashOfArrays{IreferenceTheSameArrayAsOne} = \@arrayOne ; $HashOfArrays{MyValueIsThatOfOneAsWell} = \@arrayOne ;
## to create an array reference, just type \@array
## or [ @array ]
##  [ @array ] does not equal \@array
## see perlref for more info
Answer: How do I make a hash of arrays?
contributed by Doraemon

my $array = []; #create new anonymous array ref push (@$array, '11'); push (@$array, '12'); push (@$array, '13'); $hash{'first'} = $array; # add $array = []; #create a new anon-array push (@$array, '21'); push (@$array, '22'); push (@$array, '23'); $hash{'second'} = $array; # add print "Hash content\n"; foreach $k (keys %hash) { foreach (@{$hash{$k}}) { print " : $_"; } print "\n"; }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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 wandering the Monastery: (8)
    As of 2015-07-02 22:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (46 votes), past polls