in reply to Re^3: Array value changing for some reason
in thread Array value changing for some reason
Whats the difference betweenuse strict; use warnings; problem([1,2,3]); sub problem { print2DArray(@_); #Output: 1 2 3 reverseArray1(@_); print2DArray(@_); #Output: 1 2 3 reverseArray2(@_); print2DArray(@_); #Output: 3 2 1 } sub reverseArray1 { my @arr = @_; for my $i (0 .. $#arr) { $arr[$i] = [reverse @{$arr[$i]}]; } } sub reverseArray2 { my @arr = @_; for my $i (0 .. $#arr) { @{$arr[$i]} = reverse @{$arr[$i]}; } } sub print2DArray { for my $i (0 .. $#_) { # How does that work for the nested for lo +op? for(my $j=0;$j<scalar(@{$_[$i]});$j++){ # $#_[$i] doesn't wor +k print $_[$i][$j]," "; } print "\n"; } }
and$arr[$i] = [reverse @{$arr[$i]}];
?@{$arr[$i]} = reverse @{$arr[$i]};
I still am quite new to perl, and I appreciate all the comments!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Array value changing for some reason
by poj (Abbot) on Jan 01, 2019 at 16:34 UTC |