Your skill will accomplishwhat the force of many cannot PerlMonks

### Re^2: Array value changing for some reason

by Silt (Novice)
 on Dec 31, 2018 at 23:23 UTC ( #1227851=note: print w/replies, xml ) Need Help??

in reply to Re: Array value changing for some reason
in thread Array value changing for some reason

Thanks, that helped! Even tough I don't understand what "for my \$i (0 .. \$#arr)" means, changing
```sub reverseArray {
for(my \$i=0;\$i<scalar(@_);\$i++){
@{\$_[\$i]} = reverse @{\$_[\$i]};
}
}
to
```sub reverseArray {
my @arr = @_;
for(my \$i=0;\$i<scalar(@arr);\$i++){
\$arr[\$i] = [reverse @{\$arr[\$i]}];
}
}
did the trick!

So, if I understand it correctly, a subroutine can change variables from the function it was called from.

Replies are listed 'Best First'.
Re^3: Array value changing for some reason
by kschwab (Vicar) on Dec 31, 2018 at 23:37 UTC

Even though I don't understand what "for my \$i (0 .. \$#arr)"

\$#array is the index of the last element of @array. Which is the same as the scalar(@array)-1;

a subroutine can change variables from the function it was called from

Yes, if you modify @_, it changes for the caller.

\$#array is the index of the last element of @array. Which is the same as the scalar(@array)-1;

Depending on which version of perl and the value of \$[.     :)

What I don't understand is why I get different results in this code:
```use 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";
}
}
Whats the difference between
```\$arr[\$i] = [reverse @{\$arr[\$i]}];
and
```@{\$arr[\$i]} = reverse @{\$arr[\$i]};
?

I still am quite new to perl, and I appreciate all the comments!
\$arr[\$i] = [reverse @{\$arr[\$i]}];

assigns a new reference (by creating a new anonymous array) to \$arr[\$i] replacing the reference copied from @_. Without that reference you can't change the contents of @_

Whereas @{\$arr[\$i]} = reverse @{\$arr[\$i]} uses the reference copied from @_ to change the existing array elements.

`# \$#_[\$i] doesn't work`
```sub print2DArray {
for my \$i (0 .. \$#_) {
for my \$j (0 .. \$#{\$_[\$i]}){
print \$_[\$i][\$j]," ";
}
print "\n";
}
}
or
```sub print2DArray7 {
for (@_) {
print join " ",@\$_;
print "\n";
}
}
poj
Re^3: Array value changing for some reason
by AnomalousMonk (Archbishop) on Jan 01, 2019 at 00:56 UTC
... I don't understand what "for my \$i (0 .. \$#arr)" means ...

The other point to remember about this type of loop (a so-called Perl-style for-loop) is that is "topicalizes" (see "topic" in perlglossary)  \$i or whatever variable you specify, or implicitly  \$_ if you don't explicitly specify any variable, to each element of the loop list; see Foreach Loops in perlsyn. Also note that foreach and for are exactly synonymous and completely interchangeable keywords in Perl; the differing behavior of Perl- and C-style for/foreach-loops is determined by the syntax of the loop list expression.

Give a man a fish:  <%-{-{-{-<

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1227851]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2022-12-03 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?