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";
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];
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
Regards,
Niek.