Elements of array ref used in multiple hashes not shown

84 views Asked by At

In the following code when the contents of array are printed key3 of hashref2 does not have the desired values (What I wan to achieve is hashref1 to have an array in key3 with value1 and hashref2 to have an array in key3 with value2).

In the code flow I need to first populated hashrefs and then push data to the subarray.

Can you please advice if the use of reference to subarray is the right way to go here?

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @array;
my @subarray;


# Puash to array
my $hashref1 = {
  key1 => 'value1_1',
  key2 => 'value1_2',
  key3 => \@subarray
};
push (@array, $hashref1);

# push some data in subarray
push (@subarray, 'value1');

# clear subarray
splice(@subarray);

# Puash to array
my $hashref2 = {
  key1 => 'value2_1',
  key2 => 'value2_2',
  key3 => \@subarray
};
push (@array, $hashref2);

# push some data in subarray
push (@subarray, 'value2');

print  "hashref1:\n".Dumper($hashref1);
print  "hashref2:\n".Dumper($hashref2);

print  "array:\n".Dumper(@array);

out:

hashref1:
$VAR1 = {
          'key2' => 'value1_2',
          'key1' => 'value1_1',
          'key3' => [
                      'value2'
                    ]
        };
hashref2:
$VAR1 = {
          'key2' => 'value2_2',
          'key1' => 'value2_1',
          'key3' => [
                      'value2'
                    ]
        };
array:
$VAR1 = {
          'key2' => 'value1_2',
          'key1' => 'value1_1',
          'key3' => [
                      'value2'
                    ]
        };
$VAR2 = {
          'key2' => 'value2_2',
          'key1' => 'value2_1',
          'key3' => $VAR1->{'key3'}
        };
2

There are 2 answers

0
eyevan On BEST ANSWER

I guess you just don't fully understand how references work. You're basically pushing a reference to the same array, so no wonder you have exact same contents of this array.

What you need to do is to create a new array reference. Here's the proper code:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @array;
my $subarray = [];


# Puash to array
my $hashref1 = {
  key1 => 'value1_1',
  key2 => 'value1_2',
  key3 => $subarray
};
push (@array, $hashref1);

# push some data in subarray
push ( @$subarray, 'value1' );

# create a new subarray
$subarray = [];

# Puash to array
my $hashref2 = {
  key1 => 'value2_1',
  key2 => 'value2_2',
  key3 => $subarray
};
push (@array, $hashref2);

# push some data in subarray
push ( @$subarray, 'value2' );

print  "hashref1:\n".Dumper($hashref1);
print  "hashref2:\n".Dumper($hashref2);

print  "array:\n".Dumper(@array);
0
Greg Bacon On

You are storing references to the same array in all of your hash references. This means if you modify @subarray, all of the hashes that have references to it appear to change in unison.

Instead make separate shallow copies as in

@subarray = ('value2');
my $hashref2 = {
  key1 => 'value2_1',
  key2 => 'value2_2',
  key3 =>  [ @subarray ],
};
push (@array, $hashref2);