Developer forum
cancel
Showing results for 
Search instead for 
Did you mean: 

Length/Size of one of the sub-arrays of a multi-dim array

dushyant
Booster
Hi
Is there a way to get the length/size of a sub-array of a multi-dimensional array?
DIM myArray[][]
myArray[1][1] = 500
myArray[1][2] = 510
myArray[2][1] = 600
myArray[2][2] = 610
myArray[2][3] = 620
myArray[3][1] = 700
myArray[3][2] = 710
myArray[3][3] = 720
myArray[3][4] = 730
VARDIM1(myArray[1]) !gives 4, but I need the size of just myArray[1], which is 2

I tried VARDIM2(myArray[1]) also but it doesn't give the required output.
32 REPLIES 32

Podolsky
Mentor
Why VARDIM1(myArray[1])?

Must be VARDIM1(myArray) and VARDIM2(myArray)

dushyant
Booster
Because I need the size of the sub-array myArray[1] (that is the value 2 in my example) and not the entire parent array myArray

Podolsky
Mentor
So, VARDIM1(myArray) will give you 4 and VARDIM2(myArray) will give you 3.

If you just need to find out it is one-dimensional or two dimensional array (i.e. one bracket or two brackets), make some sort of check:

something=1
IF VARDIM2(myArray) THEN something=2

I didn't try it, but hope it will work this way.

Podolsky
Mentor
dushyant wrote:
Because I need the size of the sub-array myArray[1] (that is the value 2 in my example) and not the entire parent array myArray
What do you call sub-array? Could you refer to GDL manual page about (and provide AC version)? Because one of us does not understand it correct.

dushyant
Booster
Podolsky wrote:
So, VARDIM1(myArray) will give you 4 and VARDIM2(myArray) will give you 3.
Yes, but I don't need that.

By sub-array, I mean this array: myArray[1]
For your understanding, other sub-arrays here are: myArray[2], myArray[3]
The main/parent array of these sub-arrays being myArray

myArray[1] is an array having two items: myArray[1][1] and myArray[1][2] , so its size is 2. I want this '2' as the size value of the array myArray[1].

Hope that clarifies it for you Podolsky.

Barry Kelly
Moderator
Podolsky wrote:
So, VARDIM1(myArray) will give you 4 and VARDIM2(myArray) will give you 3.

Actually in this case it is VARDIM1(myArray) = 3 and VARDIM2(myArray) = 4.

It is simply the dimensional size of your array - 3x4.

I am no expert on arrays, but I think because you have defined ... myArray[3][4] = 730
you have automatically defined all four fields for the second dimension in all 3 of the first dimension fields.

Does that make sense?

So although you have not defined ... myArray [1][3] or myArray [1][4], you have now by default and their values will be zero.

An array can have zero values (values of zero), but it can't have missing values.

Not sure if I have explained that well.


Barry.

One of the forum moderators.
Versions 6.5 to 25
Dell XPS- i7-6700 @ 3.4Ghz, 16GB ram, GeForce GTX 960 (2GB), Windows 10
Dell Precision 3510 - i7 6820HQ @ 2.70GHz, 16GB RAM, AMD FirePro W5130M, Windows 10

dushyant
Booster
Barry, so you mean it builds like a matrix of 3x4 cells and assigns 0 to any non-assigned cells?

Dominic Wyss
Booster
Sub-arrays all have the same length. two dimensional array are just tables with n x m cells.
Derived Types
Variables and parameters can also be arrays, and parameters can be value lists of a simple type.

Arrays are one- or two-dimensional tables of numeric and/or string values, which can be accessed directly by indexes.
http://gdl.graphisoft.com/reference-guide/rules-of-gdl-syntax

hth Dominic
AC23 CHE - macOS Catalina

dushyant
Booster
In that case, multi-dim arrays might not be helpful for what I am trying to do. Thanks guys for the help.

Can dictionaries be looped through?

Barry Kelly
Moderator
dushyant wrote:
Barry, so you mean it builds like a matrix of 3x4 cells and assigns 0 to any non-assigned cells?
I think so.

If you created an array in the parameter list it would look like this.

Barry.

One of the forum moderators.
Versions 6.5 to 25
Dell XPS- i7-6700 @ 3.4Ghz, 16GB ram, GeForce GTX 960 (2GB), Windows 10
Dell Precision 3510 - i7 6820HQ @ 2.70GHz, 16GB RAM, AMD FirePro W5130M, Windows 10

dushyant
Booster
Got it Barry, thanks.

Just wondering if we can loop through the values of a dictionary, like we do through an array:
FOR i = 1 TO VARDIM1(myArray) ...

Barry Kelly
Moderator
dushyant wrote:
Got it Barry, thanks.

Just wondering if we can loop through the values of a dictionary, like we do through an array:
FOR i = 1 TO VARDIM1(myArray) ...

I have not delved into DICTIONARY yet so I have no idea.

You can loop through the array as you have shown but just ignore if the value is zero.

Barry.

One of the forum moderators.
Versions 6.5 to 25
Dell XPS- i7-6700 @ 3.4Ghz, 16GB ram, GeForce GTX 960 (2GB), Windows 10
Dell Precision 3510 - i7 6820HQ @ 2.70GHz, 16GB RAM, AMD FirePro W5130M, Windows 10

Barry Kelly
Moderator
Barry wrote:
You can loop through the array as you have shown but just ignore if the value is zero.

Something like this?
This will just print the values in each dimension but not the zero values.
FOR n = 1 TO VARDIM1(myArray)
	FOR i = 1 TO VARDIM2(myArray)
		IF myArray <> 0 THEN
			PRINT myArray
		ENDIF
	NEXT i
NEXT n
Barry.

One of the forum moderators.
Versions 6.5 to 25
Dell XPS- i7-6700 @ 3.4Ghz, 16GB ram, GeForce GTX 960 (2GB), Windows 10
Dell Precision 3510 - i7 6820HQ @ 2.70GHz, 16GB RAM, AMD FirePro W5130M, Windows 10

dushyant
Booster
Barry, I had tried that but it seems to still scan through the part inside the IF statement (even if the condition is false) and gives an error:
Uninitialized array element at index: [1][3]   
(as I didn't set [1][3], but only up to [1][2])

Dominic Wyss
Booster
In that case, multi-dim arrays might not be helpful for what I am trying to do. Thanks guys for the help.

Can dictionaries be looped through?
No. Unfortunately not.
and keys can't be reached by variables... I suggested it, but graphisoft refused
AC23 CHE - macOS Catalina

Podolsky
Mentor
Just to clear the situation. Do not exist any sub-arrays. Exist one-dimensional arrays: myArray[], myArray[1], myArray[2], myArray[3], myArray[4]...
And two-dimensional arrays: myArray[][], myArray[1][1], myArray[1][2], myArray[2][1], myArray[2][2]...
If you refer two-dimensional array in your script as one dimensional - script won't work and will give an error. That means if you have let say 3x4 array: myArray[3][4] you cannot use anywhere in the script myArray[1]

And, by the way, three-dimensional arrays in mathematic called 'matrix' - like movie. It's when you have z to your array. Just imagine - array tables stack on top of each other. Or multipage excel spreadsheet...

Barry Kelly
Moderator
dushyant wrote:
Barry, I had tried that but it seems to still scan through the part inside the IF statement (even if the condition is false) and gives an error:
Uninitialized array element at index: [1][3]   
(as I didn't set [1][3], but only up to [1][2])

This is all I have written in the Master script.
No parameters in the parameter list and nothing in any other script.
The just open the 2D or 3D preview and it will print ... 500, 510, 600, 610, 700, 710, 720, 730
No error messages that I see.

Barry.

One of the forum moderators.
Versions 6.5 to 25
Dell XPS- i7-6700 @ 3.4Ghz, 16GB ram, GeForce GTX 960 (2GB), Windows 10
Dell Precision 3510 - i7 6820HQ @ 2.70GHz, 16GB RAM, AMD FirePro W5130M, Windows 10

dushyant
Booster
Strange, I get this error..

Barry Kelly
Moderator
dushyant wrote:
Strange, I get this error..

What version of Archicad are you using?
I did this in 22 and just checked in 25 and still no error messages.

Barry.

One of the forum moderators.
Versions 6.5 to 25
Dell XPS- i7-6700 @ 3.4Ghz, 16GB ram, GeForce GTX 960 (2GB), Windows 10
Dell Precision 3510 - i7 6820HQ @ 2.70GHz, 16GB RAM, AMD FirePro W5130M, Windows 10

Still looking?

Browse more topics

Back to forum

See latest solutions

Accepted solutions

Start a new discussion!