In sub merge
, you have two array refs: $auxref
and $aref
.
And you're accessing the array elements as though they were ordinary arrays (i.e. $aref[0]
) but as they are array references, you need to dereference with an arrow first: $aref->[0]
.
Adding use strict;
and use warnings;
to the top of your script should have weeded out these errors though?
Arrays
my @arr = (1, 2, 3, 4);
$arr[0] = 5;
push @arr, 6;
# @arr = (5, 2, 3, 4, 6)
Array References
my $arr = [1,2,3];
$arr->[0] = 5;
push @$arr, 6;
# $arr = [5, 2, 3, 4, 6];
2D arrays of array references
my @arr = ([1, 2], [3, 4]);
print $arr[0][1]; # identical to $arr[0]->[1];
push @{$arr[1]}, 5;
# @arr = ([1, 2], [3, 4, 5]);
2D arrayref of array references
my $arr = [[1, 2], [3, 4]];
print $arr->[0][1]; # identical to $arr->[0]->[1];
push @{$arr->[1]}, 5;
# $arr = [[1, 2], [3, 4, 5]];
2D Array of arrays
...can't exist because an array can only hold scalars
my @arr = ((1, 2), (3, 4));
# @arr = (1, 2, 3, 4);