Notifications
Clear all

[Closed] creating sorted arrays automatically…?

hey guys, I’m facing a little problem ( I’m quite a newbie when it comes to scripting )
I was wondering if anyone could help with this:
Let’s say I have a few objects in my scene named something like this:
A_10, A_20, A_100, B_30, B_60, C_02, C_100, D_100, D_50, D_35

is there a way to select those objects, and put all the ones sharing the first part of the name in an array automatically, as in, so that it basically sorts them out according to the sufix? :

arr1=#(A_10,A_20,A_100)
arr2 =#(B_30, B_60)
arr3= #(C_02, c_100)
arr4=#(D_100, D_50, D_35)

any help really appreciated! thanks!

5 Replies
 lo1
(
delete objects 
for s in #("A_10", "A_20", "A_100", "B_30", "B_60", "C_02", "C_100", "D_100", "D_50", "D_35") do
(
	point name:s
)
local arr = #()
for o in objects do
(
	local parts = filterString o.name "_"
	if (parts.count == 2) do
	(
		local subArr = (for a in arr where a.v1 == parts[1] collect a)[1]
		if (subArr == undefined) then
		(
			append arr (dataPair parts[1] #(o))
		)
		else
		(
			append subArr.v2 o
		)
	)	
)

print arr
)

Sigh… With LINQ this would be a one-liner.

hey Lo, that is very helpful, thanks a lot!

I’ve tested ur script and now whenever I run it I get an array <arr> with sub array if i understood correctly?
I’m trying to access the subarrays but throws me an error, so by doing
print arr[1][1] for example, i get this:
No ““get”” function for (DataPair “b” #($Editable_Poly:b_100 @ [253.726379,19.181978,0.000000], $Editable_Poly:b_050 @ [309.090637,19.181978,0.000000]))

any idea why that is happening?
thanks a lot man appreciated the help!

 lo1

In my example, each subarray is a datapair object, consisting of a string which contains the prefix, and an array of nodes.

So to get the first node of the first subarray, you’d use arr[1].v2[1]

ooh ok, i’ve never used these pair and datapair things, I have no idea what they are, I’ll check on the reference guide, thanks for the quick reply!

 lo1

if you don’t need the prefix kept with the array, you could get rid of them using this line after the grouping code:

arr = for a in arr collect a.v2

and then you just get an array of arrays of nodes.