Forum > General

array of array of type?

(1/4) > >>

xixixi:
How can I make a procedure accept a multidimensional open array? Here's what I tried; see the procedure Clear:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program TestArrayOfArray;{$mode OBJFPC}type   Country = (Congo, Kenya, Tanzania);   Animal  = (lion, giraffe, zebra);   Biome   = (forest, rainforest, savannah);    TIntArr = array of integer;   TIntArrArr = array of TIntArr;    TCxA = array [Country, Animal] of integer; var   cxa: array [Country, Animal] of integer;   cxa2: TCxA;   cxb: array [Biome, Animal] of integer; procedure Clear (var a: { array of array of integer  (* 1 *) }                        { array of TIntArr         (* 2 *) }                         TIntArrArr               (* 3 *)                      );var   i, j: integer;begin   for i := Low(a) to High(a) do      for j := Low(a[i]) to High(a[i]) do         a[i][j] := 0;end; begin   Clear(cxa);   Clear(cxa2);   Clear(cxb);end. 
When I try 1 (array of array of integer), I get:

--- Code: ---taa.pas(18,34) Error: Type identifier expected                     
taa.pas(18,34) Fatal: Syntax error, ")" expected but "ARRAY" found
--- End code ---

When I try 2 (array of TIntArr), I get:

--- Code: ---taa.pas(31,13) Error: Call by var for arg no. 1 has to match exactly: Got "Array[Country] Of Array[Animal] Of LongInt" expected "{Open} Array Of TIntArr"
taa.pas(32,14) Error: Call by var for arg no. 1 has to match exactly: Got "TCxA" expected "{Open} Array Of TIntArr"
taa.pas(33,13) Error: Call by var for arg no. 1 has to match exactly: Got "Array[Biome] Of Array[Animal] Of LongInt" expected "{Open} Array Of TIntArr"
--- End code ---

When I try 3 (TIntArrArr), I get:

--- Code: ---taa.pas(31,13) Error: Call by var for arg no. 1 has to match exactly: Got "Array[Country] Of Array[Animal] Of LongInt" expected "TI
ntArrArr"                                                                                                                         
taa.pas(32,14) Error: Call by var for arg no. 1 has to match exactly: Got "TCxA" expected "TIntArrArr"                             
taa.pas(33,13) Error: Call by var for arg no. 1 has to match exactly: Got "Array[Biome] Of Array[Animal] Of LongInt" expected "TInt
ArrArr"
--- End code ---

I thought the idea of "array of ..." parameters was that I can pass any array to them. What I'm doing wrong?

jamie:
is this home work or are you trying to do a bulk clear ?

howardpc:
The following skeleton code compiles and "works" in some fashion.

However, I think var parameters for arrays do not always give the results you might expect, because there seem to be persistent values still displayed, and the chain of pocedures somehow is not fully executed. So there are bugs to be eliminated for sure.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program TestArrayOfArray; {$mode objfpc}{$H+}{$IfDef windows} {$AppType console} {$EndIf} type    Country = (Congo, Kenya, Tanzania);   Animal  = (lion, giraffe, zebra);   Biome   = (forest, rainforest, savannah);     TCxA = array [Country, Animal] of Integer;   TCxB = array[Biome, Animal] of Integer; var   cxa:  TCxA = ((0, 1, 2), (2, 3, 4), (5, 6, 7));   cxa2: TCxA = ((10, 11, 12), (13,14,15), (16,17,18));   cxb:  TCxB = ((100, 101, 102), (1010, 1020, 1030), (-1, -2, -3));  procedure DisplayA(aCxA: TCxA);var  a: Animal;  c: Country;begin  for c := Low(aCxA) to High(aCxA) do    begin      WriteLn(c);      for a := Low(aCxA[c]) to High(aCxA[c]) do        Write(aCxA[c][a], ', ');      WriteLn;    end;end; procedure DisplayB(aCxB: TCxB);var  a: Animal;  b: Biome;begin  for b := Low(aCxB) to High(aCxB) do    begin      WriteLn(b);       for a := Low(aCxB[b]) to High(aCxB[b]) do        Write(aCxB[b][a], ', ');      WriteLn;    end;end; procedure ClearA (var aCxA: TCxA);begin  aCxA := Default(TCxA);end; procedure ClearB (var aCxB: TCxB);begin   aCxB := Default(TCxB);end; begin   DisplayA(cxa);   ClearA(cxa);   WriteLn(#10'------- after ClearA');   DisplayA(cxa);    DisplayA(cxa2);   ClearA(cxa2);   WriteLn(#10'------- after ClearA');   DisplayA(cxa2);    DisplayB(cxb);   ClearB(cxb);   WriteLn(#10'------- after ClearB');   DisplayB(cxb);    ReadLn;end.

xixixi:

--- Quote from: jamie on June 01, 2023, 11:57:06 pm ---is this home work or are you trying to do a bulk clear ?

--- End quote ---

Neither, that's just a made up example. I want to know how to make open arrays work with multidimensional arrays.

xixixi:

--- Quote from: howardpc on June 02, 2023, 12:30:44 am ---The following skeleton code compiles and "works" in some fashion.

...

--- End quote ---

But you are repeating the same code for each index type, which is what I want to avoid, and what open arrays are supposedly meant to solve.

Even flattening the array would help me here, i.e., the procedure getting a unidimensional array of integer of length 9, if that were possible somehow.

Navigation

[0] Message Index

[#] Next page

Go to full version