I'm hoping someone can explain why an array with one element, when 'shifted' in a subroutine continues to contain one element once the subroutine finishes.
Consider the following:
Once run, it produces the following output:
Note that INSIDE BEFORE contains an element, and INSIDE AFTER does not (as I would expect - when the only element is shifted off the array). But as soon as the subroutine returns to the main program, the array contains an empty element (shown below OUTSIDE AFTER).
Why is this, and how can I get around it?
thanks
Consider the following:
Code:
use Data::Dumper;
@arrayone = [10, [1, 2, 3]];
@arraytwo = ();
$number = 10;
print "OUTSIDE BEFORE\n======\n".Dumper(@arrayone)."\n";
&DoStuff;
print "======\nOUTSIDE AFTER\n======\n".Dumper(@arrayone)."\n";
sub DoStuff {
if (@arrayone) {
while ($arrayone[0][0] == $number) {
print "======\nINSIDE BEFORE\n======\n".Dumper(@arrayone)."\n";
my $temparr = shift(@arrayone);
print "======\nINSIDE AFTER\n======\n".Dumper(@arrayone)."\n";
push (@arraytwo, $temparr->[1]);
}}}
Once run, it produces the following output:
Code:
OUTSIDE BEFORE
======
$VAR1 = [
10,
[
1,
2,
3
]
];
======
INSIDE BEFORE
======
$VAR1 = [
10,
[
1,
2,
3
]
];
======
INSIDE AFTER
======
======
OUTSIDE AFTER
======
$VAR1 = [];
Note that INSIDE BEFORE contains an element, and INSIDE AFTER does not (as I would expect - when the only element is shifted off the array). But as soon as the subroutine returns to the main program, the array contains an empty element (shown below OUTSIDE AFTER).
Why is this, and how can I get around it?
thanks