I do not understand what is going on with this short segment of code.
I have put the output and comments about it in the middle, after the print statements which produce it.
my @regions = (
0,
1,
2,
[ 0, new Point(3,1), new Point(3,2), new Point(3,3) ],
4,
5 );
$reg1 = 3;
print "1: $regions[$reg1] \n";
@reg1 = @{regions[$reg1]};
print "2: $reg1[0]\n";
print "3: $reg1[0][1]\n";
print "4: ", Point::stringPoint($reg1[0][1]), "\n";
# HERE IS THE OUTPUT from the above print statements, with comments about my confusion appended
1: ARRAY(0xe8b0e0) # ok - element 3 of @regions is an array, as expected.
2: ARRAY(0xe8b0e0) # It appears to be an array of one element, which is itself. ???
3: Point=HASH(0xfda5e0) # We have go 'down' one more level to get what we want - that makes no sense
4: 3,1 # Yes, there it is
package Point;
sub new {
my $class = shift;
my $self = {
x => shift,
y => shift
};
bless $self, $class;
return $self;
}
sub stringPoint
{
my $p = shift;
return "$p->{x},$p->{y}";
}
" Code related to new question (with output) " ;
The real question I have is this:
How to work directly and conveniently with an array,
which is inside another array (not a copy of it) ?
Is the only way to do that by (always) de-referencing a reference?
Such as in the two non-working examples which follow.
Here is what I tried:
my $ref1 = \@{$regions[3]};
@{$ref1}[2] = new Point(4, 5); # changes original array
print1Region(3, $ref1);
# OUTPUT = (3,1) (4,5) (3,3)
my @arr1 = @{$ref1};
$arr1[1] = new Point(2,6); # does not
print1Region(3, $ref1);
# OUTPUT = (3,1) (4,5) (3,3)
$ref1[0] = new Point(1,4); # does not
print1Region(3, $ref1);
# OUTPUT = (3,1) (4,5) (3,3)
@{regions[$reg1]}- Sure about that? Try changing it to@{$regions[$reg1]}.use warnings;like you always should, perl would have warned you abut that line.