Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Values in array may change

0 views
Skip to first unread message

Niek Oosterlaak

unread,
Apr 22, 2004, 4:54:48 AM4/22/04
to begi...@perl.org
Every now and again I like to program for fun. In this case it is a
simulation. The basis is a grid that holds things like altitude,
pieces and things like that. To hold all data in a way I can remember
everything easily, I chose to put all in a multi dimensional array.
If there is a better way, please do tell. Loading the grid from file
gave me a surprise. The values in the array do not seem to be static.
The change seems to occur after leaving the Reading loop. In this
loop values do not change, calling them after the loop some values
will have changed in a regular manner. References give similar
results

Executing the program below gives a lot of differences. There is a
discernable patern in where those differences occur which leads me to
believe that perl is not at fault here, but me. (As I have sufficient
trust in my programming abillities, that would have been the logical
conclusion anyway.) Especially as another perl version on a linux
machine exhibits the same behaviour. The Perl verion used here is
v5.8.1 built for MSWin32-x86-multi-thread

Question: what am I doing wrong here?


my @GridMulti; # Multi dimensional array: playing field
my $GM_Height = 0; # Height of terrain in units above 0
my $MaxX = 80; # Number of field columns
my $MaxY = 70; # Number of field rows
my $MaxFields = (($MaxX + 1) * ($MaxY + 1)) - 1;
# Number of fields

my @CheckArray1; # First Check Array
my @CheckArray2; # Second Check Array

my $Differences = 0; # Number of differences


#Reading loop. Using rand here to prevent a extremely long post
for my $xiter (0 .. $MaxX) {
for my $yiter (0 .. $MaxY) {
$GridMulti[$xiter . $yiter . $GM_Height] = int (rand 256);
push @CheckArray1, $GridMulti[$xiter . $yiter . $GM_Height];
}
}

#CheckLoop
for my $xiter (0 .. $MaxX) {
for my $yiter (0 .. $MaxY) {
push @CheckArray2, $GridMulti[$xiter . $yiter . $GM_Height];
}
}

for my $iter (0 .. $MaxFields) {
if ($CheckArray1[$iter] != $CheckArray2[$iter]) {
$Differences++;
}
}

print "\n$Differences differences\n";

Randy W. Sims

unread,
Apr 22, 2004, 5:54:21 AM4/22/04
to Niek Oosterlaak, begi...@perl.org
Niek Oosterlaak wrote:
> Every now and again I like to program for fun. In this case it is a
> simulation. The basis is a grid that holds things like altitude,
> pieces and things like that. To hold all data in a way I can remember
> everything easily, I chose to put all in a multi dimensional array.
> If there is a better way, please do tell. Loading the grid from file
> gave me a surprise. The values in the array do not seem to be static.
> The change seems to occur after leaving the Reading loop. In this
> loop values do not change, calling them after the loop some values
> will have changed in a regular manner. References give similar
> results
>
> Executing the program below gives a lot of differences. There is a
> discernable patern in where those differences occur which leads me to
> believe that perl is not at fault here, but me. (As I have sufficient
> trust in my programming abillities, that would have been the logical
> conclusion anyway.) Especially as another perl version on a linux
> machine exhibits the same behaviour. The Perl verion used here is
> v5.8.1 built for MSWin32-x86-multi-thread
>
> Question: what am I doing wrong here?
>

For multidimensional arrays, the preferred syntax is:

my @a;
$a[$x][$y][$z] = 'value';

see perldoc perllol

corrected code below.

Randy.

-----><8-----
use strict;
use warnings;

my @GridMulti; # Multi dimensional array: playing field
my $GM_Height = 0; # Height of terrain in units above 0
my $MaxX = 80; # Number of field columns
my $MaxY = 70; # Number of field rows
my $MaxFields = (($MaxX + 1) * ($MaxY + 1)) - 1;
# Number of fields

my @CheckArray1; # First Check Array
my @CheckArray2; # Second Check Array

my $Differences = 0; # Number of differences


#Reading loop. Using rand here to prevent a extremely long post
for my $xiter (0 .. $MaxX) {
for my $yiter (0 .. $MaxY) {

$GridMulti[$xiter][$yiter][$GM_Height] = int (rand 256);
push @CheckArray1, $GridMulti[$xiter][$yiter][$GM_Height];
}
}

#CheckLoop
for my $xiter (0 .. $MaxX) {
for my $yiter (0 .. $MaxY) {

push @CheckArray2, $GridMulti[$xiter][$yiter][$GM_Height];

John W. Krahn

unread,
Apr 22, 2004, 7:16:02 AM4/22/04
to begi...@perl.org
Niek Oosterlaak wrote:
>
> Every now and again I like to program for fun. In this case it is a
> simulation. The basis is a grid that holds things like altitude,
> pieces and things like that. To hold all data in a way I can remember
> everything easily, I chose to put all in a multi dimensional array.
> If there is a better way, please do tell. Loading the grid from file
> gave me a surprise. The values in the array do not seem to be static.
> The change seems to occur after leaving the Reading loop. In this
> loop values do not change, calling them after the loop some values
> will have changed in a regular manner. References give similar
> results
>
> Executing the program below gives a lot of differences. There is a
> discernable patern in where those differences occur which leads me to
> believe that perl is not at fault here, but me. (As I have sufficient
> trust in my programming abillities, that would have been the logical
> conclusion anyway.) Especially as another perl version on a linux
> machine exhibits the same behaviour. The Perl verion used here is
> v5.8.1 built for MSWin32-x86-multi-thread

Well, you didn't describe the pattern but on my computer the anomaly
shows up when $xiter is in the range 0 to 7 AND $yiter is in the range
10 to 70. This looks like a bug in perl. BTW I am running Perl 5.6.0
on Linux.


> Question: what am I doing wrong here?
>
> my @GridMulti; # Multi dimensional array: playing field
> my $GM_Height = 0; # Height of terrain in units above 0
> my $MaxX = 80; # Number of field columns
> my $MaxY = 70; # Number of field rows
> my $MaxFields = (($MaxX + 1) * ($MaxY + 1)) - 1;
> # Number of fields
>
> my @CheckArray1; # First Check Array
> my @CheckArray2; # Second Check Array
>
> my $Differences = 0; # Number of differences
>
> #Reading loop. Using rand here to prevent a extremely long post
> for my $xiter (0 .. $MaxX) {
> for my $yiter (0 .. $MaxY) {
> $GridMulti[$xiter . $yiter . $GM_Height] = int (rand 256);
> push @CheckArray1, $GridMulti[$xiter . $yiter . $GM_Height];

You say that you are using a multi-dimensional array but that is not it,
you are concatenating the numbers together in a single dimensional
array. If you do use a multi-dimensional array it will solve your
problem:

$GridMulti[ $xiter ][ $yiter ][ $GM_Height ] = int (rand 256);
push @CheckArray1, $GridMulti[ $xiter ][ $yiter ][ $GM_Height ];


> }
> }
>
> #CheckLoop
> for my $xiter (0 .. $MaxX) {
> for my $yiter (0 .. $MaxY) {
> push @CheckArray2, $GridMulti[$xiter . $yiter . $GM_Height];

push @CheckArray2, $GridMulti[ $xiter ][ $yiter ][ $GM_Height ];


> }
> }
>
> for my $iter (0 .. $MaxFields) {

You don't really need the $MaxFields variable:

for my $iter ( 0 .. $#CheckArray1 ) {


> if ($CheckArray1[$iter] != $CheckArray2[$iter]) {
> $Differences++;
> }
> }
>
> print "\n$Differences differences\n";

John
--
use Perl;
program
fulfillment

Niek Oosterlaak

unread,
Apr 22, 2004, 7:34:48 AM4/22/04
to begi...@perl.org
Randy W. Sims wrote:
> > Question: what am I doing wrong here?
> >
>
> For multidimensional arrays, the preferred syntax is:
>
> my @a;
> $a[$x][$y][$z] = 'value';
>
> see perldoc perllol
Thanks. Works as expected. Sorry for asking something I should have
looked up. Another programming language's syntax interfered.

Regards,
Niek.

0 new messages