Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Array value changing for some reason

by choroba (Cardinal)
on Dec 31, 2018 at 22:42 UTC ( [id://1227849]=note: print w/replies, xml ) Need Help??


in reply to Array value changing for some reason

return doesn't change @_.

@_ is an array of aliases to the actual arguments, so changing @_ changes the actual arguments.

sub { ++$_ for @_ }->(my @arr = 'a' .. 'c'); print "@arr"; # Output: b c d

In your case, it's bit more complex. Changing the function to

sub reverseArray { my @arr = @_; for my $i (0 .. $#arr) { @{ $arr[$i] } = reverse @{ $arr[$i] }; } }
wouldn't have helped, as you're working with an array of arrays. The inner arrays are represented as references, so even if you store the references in a different @arr, they still refer to the same inner arrays.

To keep the array unchanged, only assign to the copy, don't change the references from the original array.

sub reverseArray { my @arr = @_; for my $i (0 .. $#arr) { $arr[$i] = [ reverse @{ $arr[$i] } ]; } }
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Array value changing for some reason
by Silt (Novice) on Dec 31, 2018 at 23:23 UTC
    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.

      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!
      ... 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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1227849]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-20 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found