Notifications
Clear all

[Closed] Comparing Arrays

Why is it that if I need to simply know whether 2 arrays are equal, a “==” doesn’t work?

I mean

aaa = #(1,2,3)
bbb = #(1,2,3)

aaa == bbb
results false.

So, I always have to check each element of these arrays.

Why can’t these guys be compared like other variables?
Any explanations regarding this is greatly appreciated.
-shibu

11 Replies

not sure aaaaCHoooo but thin that a vairable on ly contains one item but an array can contain any number of indexes.

I dont have max on this machine but you maybe could try storing the array in a variable and do the comparison on the variables…

I am not sure what kind of array you have, and I am fairly new to MaxScript, but I have done internet programming before.

Why not make a function that will use the array values in order (to make it a string) and then use ==.

Taking a stabb in the dark.

Hope it helped.
Jon

Actually, I know the work arounds to compare 2 arrays.
What I needed to know is the reason why arrays cannot be compared like float or integer variables.

thanks
shibu

I thinks that’s one to field to bobo, but it’s not really too much of a task to write:


if (a as string) == (b as string) then ...

It’s not technically false to compare two arrays, it’s just undefined.

I had never tried the “as string” method …
thanks erilaz

Just a note, if you only have number values in the array you can use it as a bitarray instead and say: if ((a – b) as array).count == 0 and ((b – a) as array).count == 0 do…

Something like Erilaz suggested should be good otherwise, just remeber to check if they both have the same .count first since otherwise you could have 2 almost equal arrays but one has more values.

CML

 PEN

If you think about more complicated senarios you can see why doing an == on two arrays isn’t possible without checking each item agaist the other.

Array’s in Max script unlike other packages can hold any type of value and any mix of types. What if you had #(1,#(2,#(3))? The first array has two items and the second is an array that has two items and the second is an array again. Also, what if the items in the array were nested structs in a binary tree?

Well, now we are getting into the philosophical discussion

Maybe this is the reason :

In c, when you create a array variable, the variable doesn’t directly store the array value, but its kind of a pointer to the mem location of the first element of the array.
So, even if the contents of 2 arrays be equal, the mem locations of the 2 are not the same .

I guess, when we do a == comparison between 2 arrays, it checks whether the locations are same, doesn’t look into whats stored in these locations.

i could be completely off track !

shibu

2 Replies
(@f97ao)
Joined: 11 months ago

Posts: 0

This is correct. Arrays work as references to memory, so you are trying to compare memory addresses pretty much.

I use several techniques for doing comparison. Most of the time an as string works fine. Sometimes you have to loop over everything.

/Andreas

(@halfvector)
Joined: 11 months ago

Posts: 0

If MAXScript works the same way than C or C++ in this case (and I think it does), then you are absolutely correct.