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
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>