'Pushing unique entry into an array within an array of arrays actually pushes entry into every array in the array of arrays?
Given the following array of arrays:
#!/usr/local/bin/perl -w
use strict;
use warnings;
my $test1 = [['1005','bak','lah','blasck','reomhol'],
['1010','name','turd','furguson','reomhol'],
['1055','a','b','c','reomhol']];
my $test2 = [['1010','name','/home/roost/chicken.pm'],
['1010','name','/home/roost/chicken2.pm'],
['1010','name','/home/roost/chicken3.pm']];
my $match_array = [];
my $nomatch_arr = [];
I have created an array of arrays ($match_array) by comparing $test1 and $test2. If the first element of each array in $test1 matches the first element of each array in $test2 then an entry will be pushed into $match_array. Therefore there should be 3 duplicate entries in . I then step through $test2 and $match_arr at the same time, trying to push a unique "location" into each array in $match_array.
my $flag = 0;
#foreach my $i (@$test1)
#my $tot = @$test1;
#$tot -= 1;
foreach my $i ( @$test1 )
{
#print "line 25: $$i[0]\n\n\n";
my $test2scalar = @$test2;
for (my $j=0;$j<$test2scalar;$j++)
{
#print "line 29: $test2->[$j][0]\n";
if($$i[0] == $test2->[$j][0])
{
push(@$match_array,$i);
$flag += 1;
}
}
if ($flag == 0)
{
push(@$nomatch_arr,$i);
}
}
print "------------------------Initial Output----------------------------\n";
for (my $k=0;$k<@$match_array;$k++)
{
print "$k-->@{$match_array->[$k]}\n";
}
print "------------------------Begin Push----------------------------\n";
for (my $count=0;$count<@$test2;$count++)
{
print "$test2->[$count][2]\n";
print "$count => @{$match_array->[$count]}\n";
push(@{$match_array->[$count]}, $test2->[$count][2]);
}
print "------------------------Final Output----------------------------\n";
for (my $k=0;$k<@$match_array;$k++)
{
print "$k-->@{$match_array->[$k]}\n";
}
Why is it then that when push(@{$match_array->[$count]}, $test2->[$count][2]); is used,it pushes the unique location into every array of $match_array?
OUTPUT:
------------------------Initial Output----------------------------
0-->1010 name turd furguson reomhol
1-->1010 name turd furguson reomhol
2-->1010 name turd furguson reomhol
------------------------Begin Push----------------------------
unique location: /home/roost/chicken.pm
array element: 0 => 1010 name turd furguson reomhol
unique location: /home/roost/chicken2.pm
array element: 1 => 1010 name turd furguson reomhol /home/roost/chicken.pm
unique location: /home/roost/chicken3.pm
array element: 2 => 1010 name turd furguson reomhol /home/roost/chicken.pm /home/roost/chicken2.pm
------------------------Final Output----------------------------
0-->1010 name turd furguson reomhol /home/roost/chicken.pm /home/roost/chicken2.pm /home/roost/chicken3.pm
1-->1010 name turd furguson reomhol /home/roost/chicken.pm /home/roost/chicken2.pm /home/roost/chicken3.pm
2-->1010 name turd furguson reomhol /home/roost/chicken.pm /home/roost/chicken2.pm /home/roost/chicken3.pm
Solution 1:[1]
I just wanted to add to this to help out the next person
An easily replicable example of this issue:
let foo = Array(3).fill([]) //fills array slots (3) with single array reference
foo[0].push('bar') //[['bar'],['bar'],['bar']]
vs
let foo = [[],[],[]] //fills array slots with three unique array references
foo[0].push('bar') // [['bar'],[],[]]
Edit to add clarity: In the first instance, you are filling the Array with a singular reference array. When you push to the referenced array, all three values are updated (since the each of the three internal arrays point to the same reference).
In the latter, you fill the array with three separately referenced arrays, so when you push to the first, only that array gets modified.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
