I am sure there are many excellent answers to this question. But I decided to write a piece of code that demonstrates various ways to add multiple values to a single hash key:

#!/usr/bin/perl use warnings; use strict; # # There are a lot of ways to associate a single hash key # With multiple values. We will demonstrate some of # these in this code: # my %hash = (); # # Using a hash element to store an (anonymous) array. # $hash{array_key} = [ 1,2,3,4,5 ]; # # using a hash element as a stack. # push @{$hash{array_key}}, 6; print "Array values are: ", join(' ',@{$hash{array_key}}), "\n"; # # Using a hash element to store a reference to an array # my @array = ( 2,3,4,5,6 ); $hash{array_ref} = \@array; # # Using a hash element to store an anonymous hash. #(Hash of hashes) # $hash{anon_hash} = { first_name => "Fred" , last_name => "Flintstone" +}; print_hash(\%{$hash{anon_hash}}); # # Using a hash element to store a reference to a hash. #(Hash of hashes) # my %neighbor = ( first_name => "Barney", last_name => "Rubble", ); $hash{hash_ref} = \%neighbor; print_hash(\%{$hash{hash_ref}}); # # Using a hash element to store an anonymous copy of a hash. #(hash of hashes) # my %wife = ( first_name => "Wilma", last_name => "Flintstone", ); $hash{hash_copy} = { %wife }; $hash{hash_copy}{first_name} = "Betty"; $hash{hash_copy}{last_name} = "Rubble"; print_hash(\%wife); print_hash(\%{$hash{hash_copy}}); sub print_hash { my $hashref = shift; print "Hash values: "; for ( sort keys %{$hashref} ) { print $$hashref{$_}, " "; } print "\n"; }
And the output is:

C:\Code>perl hash_examples.pl Array values are: 1 2 3 4 5 6 Hash values: Fred Flintstone Hash values: Barney Rubble Hash values: Wilma Flintstone Hash values: Betty Rubble

In reply to Re: add values with hash by dwm042
in thread add values with hash by steph_bow

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.