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

Barry Kelly
Moderator
Actually I get that error message if I run the 'Check Script' option.
But not when I actually view the preview or place the object.
I'm not sure if that is something that can just be ignored (I don't like ignoring error messages).
Must be something to do with the fact not all of the array values are set.

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

You have to declare all elements in the array if you are going to query the whole array with a loop.
Declare a local array (_myArray[][]) and set its size using a loop nominating every value as a default or as the actual array value. Then pass this local array to you array (myArray[][]) using parameter myArray = _myArray.
This means you have a fully declared array ready to then push your values to.
So basically you just missed the first step of forcing the array to exist with all cells holding memory.
Creator of Cadswift's parametric GDL libraries
Creator of Infinite Openings and Component Catalogues
Push the envelope & watch it bend
website: https://cadswift.com.au/
YouTube: https://www.youtube.com/user/CADSwift/playlists

dushyant
Booster
Barry, yes, the error comes on checking the script. (I'm on v24)

Kristian, that's a way around it, yes. When you say 'local array' I believe you are comparing with the parameters array.. Anyway, all my arrays in this situation are locally created. This would mean so many repeated loops and copies of arrays.

dushyant wrote:
This would mean so many repeated loops and copies of arrays.
Not so many just one to declare the whole thing (5 line loop statement) and then one populating the values with whatever logic you require. The master script of Infinite Openings is laden with declared and then populated arrays. The tool must dynamic shift between varying configurations via the catalogues and this issue was one that would break it many times until I finally ironed out the last not fully declared array, luckily some time ago.
Creator of Cadswift's parametric GDL libraries
Creator of Infinite Openings and Component Catalogues
Push the envelope & watch it bend
website: https://cadswift.com.au/
YouTube: https://www.youtube.com/user/CADSwift/playlists

also when you do declare your array, invert the loop doing for example: for i = arrayDim1 to 1 step -1
It makes it generate faster because you have already set the full dimension in the first run of the loop.
Creator of Cadswift's parametric GDL libraries
Creator of Infinite Openings and Component Catalogues
Push the envelope & watch it bend
website: https://cadswift.com.au/
YouTube: https://www.youtube.com/user/CADSwift/playlists

dushyant
Booster
Kristian wrote:
also when you do declare your array, invert the loop doing for example: for i = arrayDim1 to 1 step -1
It makes it generate faster because you have already set the full dimension in the first run of the loop.
Ok, thanks.

SenecaDesignLLC
Booster
Kristian wrote:
also when you do declare your array, invert the loop doing for example: for i = arrayDim1 to 1 step -1
It makes it generate faster because you have already set the full dimension in the first run of the loop.
Did not know this! But hell yes! Makes so much sense!
AC 24, Twinmotion current, Windows 11, Msi 10UG with rtx 3070, wacom intuos pro m, Wacom Cintiq Pro 16, razer Tartarus pro.

SenecaDesignLLC wrote:
Kristian wrote:
also when you do declare your array, invert the loop doing for example: for i = arrayDim1 to 1 step -1
It makes it generate faster because you have already set the full dimension in the first run of the loop.
Did not know this! But hell yes! Makes so much sense!
I got that bit of gold directly from Gergely Feher (when I visited GSHQ) while he was advising me on ways to speed up Infinite Openings; it made a massive difference!
Creator of Cadswift's parametric GDL libraries
Creator of Infinite Openings and Component Catalogues
Push the envelope & watch it bend
website: https://cadswift.com.au/
YouTube: https://www.youtube.com/user/CADSwift/playlists

Podolsky
Newcomer
Interesting. What else he advised in term of speeding up the GDL scripts? Please write it here - we will include it into GDL writing rules for everyone. Maybe just write all these rules in Open Source GDL post?

dushyant
Booster
Podolsky wrote:
... Maybe just write all these rules in Open Source GDL post?
What is 'Open Source GDL'? I thought there was only one GDL (by Graphisoft)

Podolsky
Newcomer
We started here (I started) the thread called Open Source GDL Project - where users can exchange GDL scripts, ideas, macros and develop new tools in open source manner.

dushyant
Booster
Ah, okay.

Peter Baksa
Graphisoft
Graphisoft
I think this might help:
dict d
dim EMPTYARRAY[]

d.array[1].subarray = EMPTYARRAY
d.array[2].subarray = EMPTYARRAY

d.array[1].subarray[1] = 11
d.array[2].subarray[1] = 21
d.array[2].subarray[2] = 22


print d, vardim1(d.array[1].subarray), vardim1(d.array[2].subarray)
dushyant wrote:
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?
Péter Baksa
Software Engineer, Library as a Platform
GRAPHISOFT SE, Budapest

Still looking?

Browse more topics

Back to forum

See latest solutions

Accepted solutions

Start a new discussion!