Notifications
Clear all

[Closed] Testing if array1 != aray2?

Im trying to test something simmillar to the following
first, check if the arrays are not equal.
Second set array2 to array 1 so they do equal, so next time it comes to the loop, it will skip the loop if they are equal.

At the moment it doesnt seem to work, im not shure if you can compare arrays, as im printing the output and both arrays are the same, but it still loops through it.

if array1 != array2 do
(

array2=array1

)

22 Replies

Maxscript does not compare the arrays when you write:

#(1,2,3)==#(1,2,3)
–>false

I think maxscript compares the pointers of the arrays.
Then if you write:

a=#(1,2,3)
b=#(1,2,3)

a==b
–>false

but if you assign the same pointer to the 2 variables, that becomes:

a=b
a==b
–> true

1 Reply
 PEN
(@pen)
Joined: 11 months ago

Posts: 0

I wasn’t aware that would work completely. I have to try that out.

 PEN

Well because MXS arrays can hold any values including other arrays comparing them in that way isn’t possible.

You will need to write a function that will compare each item in the array and if there is a nested array it will need to recurse into them and compare them as well.

okay thought i would have to do something like that.

Thanks

Sorry. I did not answer your question.

I currently use this function when I want to compare 2 arrays:

fn isSameArray array1 array2 =
	(
	if array1.count!=array2.count do return false
	local count=array1.count
	for i=1 to count do ( if array1[i]!=array2[i] do return false )
	true
	)
 PEN

In reading your post again I’m not sure what that does for you??

 PEN

And if you wanted to make it full proof you would have to make it recursive and test the nested arrays for thier values.

Oh yes… If the arrays to compare contain one array themselves my function does not work.
Thanks for the idea of recursivity, Pen.

 PEN

I think that this is working. Not tested it a whole lot but it recurses and can test by class. It needs handling for case when dealing with strings so that it can be case insensitive. Let me now if you find any errors or problems with it.

struct miscFunc
 (
 	/****************************************************
 	Compare two arrays	
 	Needs handling for strings to test for case
 	****************************************************/
 	isSame=true,
 	fn compareArrays a b recurse:false byClass:false=
 	(
 		if a.count!=b.count then 
 		(
 			return false
 		)
 		for i = 1 to a.count do
 		(
 			aClass=classOf a[i]
 			bClass=classOf b[i]
 			
 			if recurse and aClass==Array and bClass==Array then 
 			(
 				compareArrays a[i] b[i] recurse:true
 			)else
 			(
 				if aClass!=Array and bClass!=Array then
 				(
 					case byClass of
 					(
 						true:
 						(
 		 		 isSame=aClass==bClass
 	 		 		if isSame == false then (return false)
 						)
 						false:
 						(
     		    		    isSame=a[i]==b[i]
 	 		 		if isSame == false then (return false)
 						)
 					)
 				)
 			)
 		)
 		isSame
 	)
 )
 
 /* Test Area
 	miscFunc=miscFunc()
 	miscFunc.isSame
 	a=#(1,2)
 	b=#(1,2)
 	a=#(1,2,#(4,5),"crap")
 	b=#(1,2,#(4,5),"Crap")
 	miscFunc.compareArrays a b recurse:false byClass:false
 */

Hey you did it!
Thanks

Your code show me that default values exists in function… but I never succeeded in using them.
Are there default values in the functions?

If I write this function:
fn test a b c:333 =
(
format “a=% b=% c=%
” a b c
)

And I call it:
test 111 222
–> a=111 b=222 c=333

But if I decide to assign an another value than the default value, I obtain this:

test 111 222 999
– Argument count error: test wanted 2, got 3

How use this kind of argument myVariable:xxx ?
It is not clear for me.
Can anyone enlighten me on this?

Page 1 / 3