Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Array wierdness

by Corion (Patriarch)
on Apr 25, 2022 at 13:56 UTC ( [id://11143269]=note: print w/replies, xml ) Need Help??


in reply to Array wierdness

This seems to be a bug surprising side-effect of non-strict code to me. First you assign the numbers to both arrays in one go. Then, you use these numbers as (symbolic) references when you by using the arrays as multi-dimensional. Perl then uses the arrays @1, @2 etc. , and these arrays are shared between @PP and QQ. This is surprising.

Note that using 'strict' would have allowed Perl to tell you that you are implicitly upgrading/changing numbers to references:

Can't use string ("1") as an ARRAY ref while "strict refs in use at .. +.

Update: Updated my explanation to what actually happens, after a conversation with haarg

Replies are listed 'Best First'.
Re^2: Array wierdness
by lgp (Initiate) on Apr 25, 2022 at 14:30 UTC
    Thanks for the explanation. FWIW, assigning the numbers to the arrays in separate lines makes no difference.
      Just to elaborate a bit more on this, consider the code:
      my @PP = ("foo", "bar", "baz"); my @OO = ("AAA", "bar", "CCC"); $PP[1][0] = "foobar";
      What happens is that this does not modify the @PP array, instead it uses the value of $PP[1] as an array reference which points to the global(/package) variable @bar. To make this a bit clearer:
      #!/usr/bin/perl -l my @PP = ("foo", "bar", "baz"); my @OO = ("AAA", "bar", "CCC"); $PP[1][0] = "foobar"; print "\@PP: " . join(", ", @PP); print "\@OO: " . join(", ", @OO); print "\@bar: " . join(", ", @::bar);
      Output:
      @PP: foo, bar, baz
      @OO: AAA, bar, CCC
      @bar: foobar
      
      The '@PP' and '@OO' array are unmodified; the '@bar' array however was created.

      With use strict; you get the error: Can't use string ("bar") as an ARRAY ref while "strict refs" in use

      FWIW, assigning the numbers to the arrays in separate lines makes no difference.

      This is because your code's comments are wrong:

      # fill two 25 x 25 arrays with zeros, then modify a couple of cells my @PP = my @OO = (1..25);

      This does not create 25x25 arrays, this creates 1x25 arrays.

      Below shows a 1x25 (what you have) vs a 2x5:

      use strict; use warnings; use Data::Dump; my @one_by_twentyfive = (1..25); my @two_by_five = ([1..5],[1..5]); dd \@one_by_twentyfive; dd \@two_by_five; __END__ [1 .. 25] [[1 .. 5], [1 .. 5]]

      or this one populates a 25x25 array, then overrides 3 slots:

        Hello,

        you can also initialize an ArrayOfArrays setting default values using x (repetition operator in list context) like in:  @aoa = ([( 1 ) x 3]) x 3

        L*

        PS I evidently need more coffeine this morning.. see below

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      short answer

      print this on a t-shirt:

      and wear it at work. ;-)

      long answer

      $PP[1][2]=3 is a shorter notation for $PP[1]->[2] = 3

      so with

      $PP[1] == $OO[1] == 1

      what you are effectively doing is

      1->[2] = 3

      which is just populating @1

      debugger-demo perl -de0

      DB<46> 1->[2] = 3 DB<47> x @1 0 undef 1 undef 2 3 DB<48>

      For good reasons that's forbidden under strict

      DB<45> use strict; my @PP; $PP[1]=1; $PP[1][2] = 3 Can't use string ("1") as an ARRAY ref while "strict refs" in use at ( +eval 66) ...

      > FWIW, assigning the numbers to the arrays in separate lines makes no difference.

      Well I hope it's now clearer why...

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-23 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found