Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

multiple values for one key in hash

by sovixi (Novice)
on May 12, 2008 at 21:37 UTC ( #686151=perlquestion: print w/ replies, xml ) Need Help??
sovixi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Is there any chance I can assign multiple values to one key in a hash? Thanks

Comment on multiple values for one key in hash
Re: multiple values for one key in hash
by Fletch (Chancellor) on May 12, 2008 at 21:41 UTC

    Nope. One slot in a hash holds one scalar value.

    The trick is to use a scalar value that can hold more than one value instead. See perldsc and perlreftut.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: multiple values for one key in hash
by kyle (Abbot) on May 12, 2008 at 21:45 UTC

    No, but you can make the one value for a key be a reference to an array which contains multiple values.

    $h{key} = [ 'value1', 'value2' ]; push @{ $h{key} }, 'value3', 'value4'; # $h{key}->[2] eq 'value3' # ${ $h{key} }[3] eq 'value4'

    If you're not familiar with array references, see perlreftut for a start.

    You can also make the one value for a key be a string that encodes all the various values (e.g., CSV or Data::Dumper output, etc.)

Re: multiple values for one key in hash
by wade (Pilgrim) on May 12, 2008 at 22:04 UTC

    How about using a reference to an array. Here's a little ditty that builds the kind of hash for which you're looking and then prints out the results:

    use strict; use warnings; use diagnostics; use Data::Dumper; my %hash; my $key; my $value; my @data = ("foo",1, "bar",2, "foo",3, "baz",4); while ($key = shift (@data)) { $value = shift (@data); # push the value on the array push @{$hash{$key}}, $value; } print Dumper \%hash;

    Note: you have to create the anonymous reference -- you can't just push a value if the key doesn't exist.

    Update: apparently you can just push the value.

    --
    Wade
      Note: you have to create the anonymous reference -- you can't just push a value if the key doesn't exist.

      In perl you can ;-)

      use strict; use warnings; use Data::Dumper; my %hash; push @{$hash{key}}, 'value'; print Dumper \%hash; __END__ $VAR1 = { 'key' => [ 'value' ] };

      This nice feature is called "autovivification" and is quite useful most of the time.

        Son of a gun! You can just push the value. I could have sworn that this didn't work when I tried it at my old job (about 2 years ago). Is this a feature that has happened since then or have I been brain dead all this time?

        Thanks, moritz; ++

        --
        Wade
Re: multiple values for one key in hash
by Anonymous Monk on Nov 08, 2012 at 09:14 UTC
    how can i get the count of value array(multiple values are stored as an array) here?

      Just dereference the array in a scalar context:

      #! perl use strict; use warnings; my %families = (Flintstone => [ qw(Pebbles) ], Simpson => [ qw(Bart Lisa Maggie) ], Keaton => [ qw(Alex Mallory Jennifer Andy) ]); for my $name (keys %families) { my $num_children = @{ $families{$name} }; # <-- Here's where it + happens if ($num_children == 1) { print "There is $num_children child in the $name family\n"; } else { print "There are $num_children children in the $name family\n" +; } }

      Hope that helps,

      Athanasius <°(((><contra mundum

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2014-08-22 14:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (158 votes), past polls