Question

I want to add a hash to an array which is constructed from a previous hash value. Let us say, we have a hash:

( a=>1, b=>2, c=>3, d=>4)

I want to transform it to an array of hashes which would contain the following:

[
    ('key' => 'a', 'value' => 1),
    ('key' => 'b', 'value' => 2),
    ('key' => 'c', 'value' => 3),
    ('key' => 'd', 'value' => 4),
]

For doing this, I wrote the below snippet:

%hash = (a=>1,b=>2, c=>3, d=>4);
@arr = ();
foreach(keys %hash) {
    # Making a hash
    %temp = ('key' => $_, 'value' => $hash{$_});

    # Getting its reference
    $hashref = \%temp;

    # Push the reference of hash in the array
    push( @arr, $hashref);

    # Print it to know its value
    print $_.' '.$hash{$_}."\n";
}

foreach(@arr) {
    # Deref the hash
    %h = %{$_};

    # Print the element
    print 'elem: '.$h{'key'}."\n";

    # Print the reference
    print "Ref: ";
    print ref $_."\n";

    # Print the keys in hash
    print "keys in hash: ".keys %h;
    print "\n";
}

But the output contains only four entries of only one reference:

c 3
a 1
b 2
d 4
elem: d
Ref: keys in hash: 2
elem: d
Ref: keys in hash: 2
elem: d
Ref: keys in hash: 2
elem: d
Ref: keys in hash: 2

Why is it adding duplicates? Any suggestion to add the all the values ?

The code can be tried here: http://ideone.com/OIZiGZ

I have already looked at many answers and then arrived here when it did not work.

Was it helpful?

Solution

You are adding a reference to the same %temp hash each time.

Change your code to say:

my %temp = ('key' => $_, 'value' => $hash{$_});

and each time through the loop %temp will be a different hash.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top