Cancel
Showing results for 
Search instead for 
Did you mean: 

two dim array with unknown size

Pioneer
Pioneer

Hi there,

 

I have a method that gives me an array as a parameter of my method which can vary in sice. In this method I create a two dimensional array with number of entries depending on the given array.

Now I tried to make something like arrayX : integer [n,n] and arrayX : integer [,] , but that doesn't work (n is the number of entries, also given as parameter of this method).

 

Is there a method to do this I just missed? Or is there some trick/workaround I can use?

 

 

7 REPLIES 7

Re: two dim array with unknown size

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor
try arrayX: any;
this should work
Steffen Bangsow
freelance simulation specialist  
web: www.bangsow.eu
mail: steffen@bangsow.net

Re: two dim array with unknown size

Pioneer
Pioneer

Hi,

 

thanks for your quick reply, but this doesn't work either.

When I try it that way and access a specific value, e.g. [1, 2], the debugger states that this use is only possible for arrays and stuff.

And arrayX : any[] would only be a one dimensional array and throws the error that the index is out of bound.

Re: two dim array with unknown size

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

I guess we need more of your code. You need to assign a array to the variable.

e.g.

local arrayX:any;
arrayX:=buffer.contentsList;--method returns an array
print arrayX[1,1];
Steffen Bangsow
freelance simulation specialist  
web: www.bangsow.eu
mail: steffen@bangsow.net

Re: two dim array with unknown size

Pioneer
Pioneer

Ok, I guess the problem why this didn't work was, that I didn't assign a array to this variable.

 

But then again I would have to have a two dimensional array with a already known size. Don't I? But the problem is that I don't know the size and would like to initialize it depending on the size of the array I get through the parameter.

 

Here is the part of my code I have problems with:

 

(posX : any[])
is

arrC : real[6,6]; --C matrix containing distances from point to point

do

--TEST VALUES-------
posX := makeArray(makeArray(15,0), makeArray(0,25), makeArray(25, 25), makeArray(35,25), makeArray(25,45), makeArray(100,100));
--------------------
n := posX.dim;

--creating C matrix with distances from point to point
for local i := 1 to n loop
    for local j := 1 to n loop
			
        local deltaX, deltaY : real;
        deltaX := (posX[i])[1]-(posX[j])[1];
        deltaY := (posX[i])[2]-(posX[j])[2];
        arrC[i, j] := sqrt(pow(deltaX,2) + pow(deltaY,2));
			
    next;
next;

end;

arrC is the two dim array that changes denpending on the number of entries given by array posX. 

Re: two dim array with unknown size

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

I guess, you need for the result the data type table. Until now there is no way to change the dimension of a array (like redim in VB).

 

If you use the datatype table, you need so set the datatypes of the columns (from string) to real:

--(posX : any[])
is

   arrC : table; --n x n matrix containing distances from point to point
   posX:any[]; --for testing
   n,i:integer;
do
 

  --TEST VALUES-------
  posX := makeArray(makeArray(15,0), makeArray(0,25), makeArray(25, 25),     makeArray(35,25), makeArray(25,45), makeArray(100,100));
--------------------
   n := posX.dim;
   arrC.create;

--change datatypes of the matrix to real
for i:=1 to n loop
	arrC.setDataType(i,"Real");
next;


--creating C matrix with distances from point to point
for i := 1 to n loop
    for local j := 1 to n loop
			
        local deltaX, deltaY : real;
        deltaX := (posX[i])[1]-(posX[j])[1];
        deltaY := (posX[i])[2]-(posX[j])[2];
        arrC[i, j] := sqrt(pow(deltaX,2) + pow(deltaY,2));
			
    next;
next;

end;
Steffen Bangsow
freelance simulation specialist  
web: www.bangsow.eu
mail: steffen@bangsow.net

Re: two dim array with unknown size

Phenom
Phenom

how about using a table ?

 
--(posX : any[])
is

arrC : real[6,6]; --C matrix containing distances from point to point
T : table;

do
 
 local posX :any[];
 local n :integer;
 
 t.create;
--TEST VALUES-------
posX := makeArray(makeArray(15,0), makeArray(0,25), makeArray(25, 25), makeArray(35,25), makeArray(25,45), makeArray(100,100));
--------------------
n := posX.dim;
print n;
--creating C matrix with distances from point to point
for local i := 1 to n loop
    for local j := 1 to n loop
   
        local deltaX, deltaY : real;
        deltaX := (posX[i])[1]-(posX[j])[1];
        deltaY := (posX[i])[2]-(posX[j])[2];
  arrC[i, j] := sqrt(pow(deltaX,2) + pow(deltaY,2));
  
  if i> T.xdim then
   T.insertColumn(i);
   T.setDataType(i,"real");
  end;
  if J> T.ydim then
   T.insertrow(j);
  end;
  T[i,j]:=sqrt(pow(deltaX,2) + pow(deltaY,2));
    next;
next;

for local i := 1 to n loop
    for local j := 1 to n loop
  print i,chr(9),j,chr(9),T[i,j],chr(9),arrc[i,j];
 next;
 next;


end;

Re: two dim array with unknown size

Pioneer
Pioneer

Thank you. That's the solution.