lgp has asked for the wisdom of the Perl Monks concerning the following question:
When run, both arrays are the same. With the first active line changed to my @PP = my @OO; with no initialization, the code works as expected; i.e., the @PP array is unchanged. Why the difference??#!/usr/bin/perl # fill two 25 x 25 arrays with zeros, then modify a couple of cells my @PP = my @OO = (1..25); # fill PP and OO arrays with zeros for($rr = 0; $rr < 25; $rr++) { for($cc = 0; $cc < 25; $cc++) { $PP[$rr][$cc] = 0; $OO[$rr][$cc] = 0; } } # uncommenting the below shows array PP zeroed out for my $row (@PP) { print join(",", @{$row}), "\n"; } print "\nAfter:\n"; $OO[1][1]=1; # just so it will be easy to spot $OO[2][2]=2; $OO[3][3]=3; $OO[4][4]=4; # code below. shows the values inserted into array OO cells appearing +in PP cells ??? print "PP array\n"; for my $row (@PP) { print join(",", @{$row}), "\n"; } print "\nOO array\n"; for my $row (@OO) { print join(",", @{$row}), "\n"; } exit;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array wierdness
by Corion (Patriarch) on Apr 25, 2022 at 13:56 UTC | |
by lgp (Initiate) on Apr 25, 2022 at 14:30 UTC | |
by Animator (Hermit) on Apr 25, 2022 at 15:36 UTC | |
by pryrt (Abbot) on Apr 25, 2022 at 15:34 UTC | |
by Discipulus (Canon) on Apr 26, 2022 at 09:04 UTC | |
by hv (Prior) on Apr 26, 2022 at 09:13 UTC | |
by LanX (Saint) on Apr 26, 2022 at 10:25 UTC | |
|
Re: Array wierdness
by BillKSmith (Monsignor) on Apr 27, 2022 at 16:03 UTC | |
|
Re: Array wierdness
by etj (Priest) on Apr 27, 2022 at 16:04 UTC |