Notifications
Clear all

[Closed] Remove duplicates from structure?

Hi,
I have a little question about remove duplicates from structure via maxscript,below is the code:
test_array[1] and test_array[3] is the same, how to remove duplicates?

test_array=#()
struct test_struct (test_01,test_02)

append test_array (test_struct test_01:"a" test_02:"w")
append test_array (test_struct test_01:"b" test_02:"xc")
append test_array (test_struct test_01:"a" test_02:"w")
append test_array (test_struct test_01:"c" test_02:"n")
append test_array (test_struct test_01:"d" test_02:"w")

a=makeUniqueArray test_array

The output is:

    (test_struct test_01:"a" test_02:"w")
    (test_struct test_01:"b" test_02:"xc")
    (test_struct test_01:"a" test_02:"w")
    (test_struct test_01:"c" test_02:"n")
    (test_struct test_01:"d" test_02:"w")
    OK
3 Replies

If your struct doesn’t contain any nested struct this one will probably could work.

(
	fn getStructHash def =
	(
		local hash = 0
		for p in getPropNames def where (val = getHashValue (getProperty def p) hash) != undefined do hash = val
		hash
	)

	hashes = #{}
	aa = for item in test_array where ((hash = getStructHash item) > 0) collect
	(
		if hashes[ hash ] then dontCollect else 
		(
			hashes[ hash ] = true
			item
		)
	)

	for x in aa do format "%\n" x
)

Thanks too much,works like a charm!

I changed your code to fit other situation
aa = for item in test_array where ((hash = abs (getStructHash item)) > 0) collect
hope the code can help anyone.

(
	fn getStructHash def =
	(
		local hash = 0
		for p in getPropNames def where (val = getHashValue (getProperty def p) hash) != undefined do hash = val
		hash
	)

	hashes = #{}
	aa = for item in test_array where ((hash = abs (getStructHash item)) > 0) collect
	(
		if hashes[ hash ] then dontCollect else 
		(
			hashes[ hash ] = true
			item
		)
	)

	for x in aa do format "%\n" x
)