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

Hi, Below I pasted a code that I got from here for assigning multiple values to the key in perl hashes. This code is working for one and not for the other. I don't understand where it is going wrong. If anybody can help me do this, it will be hugely appreciated.
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;
The array in the two cases mentioned is different. I am getting output as below where you can see the keys not as usual. I am not able to access the keys also, basically it is not getting.

Replies are listed 'Best First'.
Re: Multiple values assigned to same key in hashes
by Random_Walk (Prior) on Feb 11, 2016 at 09:11 UTC

    I'm sorry but your post formating is rather illegible. Can you please re-format using <c>...</c> tags around code and <p>...</p> tags for paragraphs of text.

    Having said that, the code you have is a little odd but it does work for me. I've tidied it up a bit, moving the lexical (my) variables into the loop where they are used

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

    and here is the perfectly good output

    $VAR1 = { 'bar' => [ 2 ], 'baz' => [ 4 ], 'foo' => [ 1, 3 ] };
    This output is telling you that the hash has three keys, and each one holds an array reference (thus the [ ]). One of them (foo) holds two values in the referenced array, the others a single value. What are you seeing?

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: Multiple values assigned to same key in hashes
by AnomalousMonk (Archbishop) on Feb 11, 2016 at 17:10 UTC
    This code is working for one and not for the other. ... The array in the two cases mentioned is different.

    I, too, don't understand just what you want to achieve, and I'm confused by your reference to "two cases."

    However, there is a small problem in your OPed code (as cleaned up by Random_Walk above) that can be illustrated by the following dataset:

    c:\@Work\Perl>perl -wMstrict -le "use Data::Dumper; my %hash; my @data = ('0', 'zero', 'foo',1, 'bar',2, 'foo',3, 'baz',4); while (my $key = shift @data) { my $value = shift @data; push @{$hash{$key}}, $value; } print Dumper \%hash " $VAR1 = {};
    The while-loop conditional it testing the truth of the "key" being shifted out of the array, but a false string can still be a valid hash key. A better approach might be something like:
    c:\@Work\Perl>perl -wMstrict -le "use Data::Dumper; ;; my @data = ('0', 'zero', 'foo',1, 'bar',2, 'foo',3, 'baz',4); ;; my %hash; while (@data) { my $k = shift @data; die qq{no value for key '$k'} unless @data; my $v = shift @data; push @{$hash{$k}}, $v; } print Dumper \%hash " $VAR1 = { 'bar' => [ 2 ], 'baz' => [ 4 ], '0' => [ 'zero' ], 'foo' => [ 1, 3 ] };
    (Of course, this doesn't address the fact that the undefined value can be an element of the  @data array and end up as a key (update: or hash value), but that's another story.)


    Give a man a fish:  <%-{-{-{-<

    A reply falls below the community's threshold of quality. You may see it by logging in.