Writing to a single element of a nested array

Dear forum members,

I am once again struggling with my rhinoscript and have already searched the whole internet for a solution of the following problem.

I need to store data in a nested array. The data consists of strings which are devided into max. 300 groups. Each group consists of max. 100 strings. I would like to define a nested array with dimensions in the following bounds: arrStrings(0 to 300)(0 to 100).

The problem is, that I have to write the strings to the array one after the other in a loop. Reading a single element of the array is easily done by e.g. Rhino.Print arrString(51)(45).

I tried a similiar syntax to set the values of the elements, but this approach didn’t work, e.g.: arrString(51)(45)=“Test”.

I hope someone can help me. Thanks!

Does this article help?

Unfortunately this article does not show how to define a single element in a ragged array. :frowning:

Here is a simple example that shows how to assign new values to existing element in your array.
I hope this helps.

Sub Main()
	'initial array
	Dim MyArray
	MyArray = Array(Array(1, 2, 3), Array(4, 5), Array(6, 7, 8, 9))
	Rhino.Print MyArray(2)(0) 'prints 6
	
	'Assign new value
	MyArray(2)(0) = 66
	Rhino.Print MyArray(2)(0) 'prints 66
End Sub

Same example with strings…

Sub Main()
	'initial array
	Dim MyArray
	MyArray = Array(Array("A", "B", "C"), Array("D", "E"), Array("F", "G", "H", "I"))
	Rhino.Print MyArray(2)(0) 'prints F
	
	'Assign new value
	MyArray(2)(0) = "Test"
	Rhino.Print MyArray(2)(0) 'prints Test
End Sub

Is there a convenient way to assign the initial values of the array in a short syntax? Assigning the values in the shown way in case of a array with 300 x 500 = 150000 values would be crazy :grinning:

Can you please share the code of what you are doing, so I can have a look?

Hey, we are talking about 1000 lines of code and the nested array is deep inside of several loops. It wouldn’t help if i post the whole code. Instead i will try to explain what I am trying to do.

The script will be used write to several csv-files which will be used to export & import the compartmentation of a ship (rooms inside the hull) to an other calculation tool:

  • Each layer in Rhino represents a comparment. The name of the layer is written to the csv-file.

  • Each compartment is build together from multiple spaces. Each space is defined on its own child-layer. The names of the child-layers are also writted to the csv-files.

  • Each space is defined by multiple faces/planes which are defined on the child-layers.

The script consists of the following loops:

  • 1st loop-level: loop through compartments
  • 2nd loop- level: loop through spaces
  • 3rd loop-level: loop through faces

Because the comparment/space/face-information have to be sorted and structured before writing them to the csv-files, i have to save them to an array. Thats why I am trying to use a nested array:

The first index will be used for the spaces (up to 300).
The second Index will be used for the faces, which belong to each space (up to 100 per space).

Here is a basic sample to assign and retrieve items from a 2 dimensional array using loops

Sub Main()
	Dim i, j
	Dim arrNames(10,5)

	For i = 0 To 10 - 1 
		For j = 0 To 5 - 1
			'Assign value to an array item
			arrNames(i, j) = "N" & i & j     
		Next
	Next
	
	For i = 0 To 10 - 1 
		For j = 0 To 5 - 1
			'Print array item
			Rhino.Print arrNames(i, j)    
		Next
	Next
	
End Sub

There are additional utility samples that you might find useful here:

Thanks. It seems I have to use your way and use a multidimensional array. It’s quite interesting, that there is no syntax for a nested array.

These samples might also be helpful:
https://wiki.mcneel.com/developer/scriptsamples/dimbounds

Here is a sample to create jagged array dynamically. Hopefully this comes closer to what you need

Option Explicit
'Script written by Rajaa Issa
'Script copyrighted by Robert McNeel & Associates
'Script version Tuesday, May 25, 2021 11:14:30 AM

Call Main()
Sub Main()
	Dim MyArray() 
	Dim SubArray()
	Dim len, i, j, b
	
	'x = length of 1st dimention of MyArray
	Dim x: x = 4
	ReDim MyArray(x)
	
	For i = 0 To x
		'len = length of 2nd dim array for each row up to 10
		len = 1 + Int(Rhino.Rnd * 10)
		ReDim SubArray(len)
		For j = 0 To len
			'Assign value to an array item
			SubArray(j) = "A-" & i & "-" & j     
		Next
		MyArray(i) = SubArray
	Next
	
	'Print valuse
	For i = 0 To x
		b = UBound(MyArray(i))
		For j = 0 To b
			'Print
			Rhino.Print MyArray(i)(j)    
		Next
	Next
	
End Sub

Thank you very much for your help. The solution from the last post should be perfect for my problem.

1 Like