En:Multidimensional arrays

Материал из Pocket Fallout для КПК на PDA PC PPC Mobile.

Multidimensional arrays

Unfortunately, by means of PFSL it is possible to create only linear arrays. However in practice frequently it is necessary to create two-dimensional arrays (in 90 % - two-dimensional, in other cases of measurements can be and more).
Two-dimensional array can be organised from one-dimensional on following algorithm :

array_size = matrix_height*matrix_height_width;
element_index = matrix_height*y + x;
Where :
x,y - axes coordinate of matrix

Further, for using convenience such arrays it is necessary to organise functions-covers for element reading/record by coordinates

Example

For example we have matrix 4x5 (two-dimensional array). It is necessary to do a cover of reading/record for this two-dimensional array.

var matrixHeight = 5; // Matrix height  
var matrixWidth = 5;
var size = matrixHeight*matrixWidth; // Array size

function GetIndex(x, y) //Index calculation
   return matrixHeight*y + x;
endfunction

function GetMyArrayElement(myArray, x, y)  //Get value
    return myArray[GetIndex(x,y)];
endfunction

procedure SetMyArrayElement(myArray, x, y, value) //Set value
    myArray[GetIndex(x,y)] = value;
endfunction
procedure TestArray()
   var myArray[size]; 
   SetMyArrayElement(myArray, 0, 1, "Hello !");
   SetMyArrayElement(myArray, 1, 1, "World !"); 
   
   var result = GetMyArrayElement(myArray, 0, 1); //  Return Hello !
endprocedure
Личные инструменты