PDA

View Full Version : Comparing Arrays


shibumenon
08-28-2005, 03:19 PM
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

Dave_Hingley
08-28-2005, 06:27 PM
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...

omega3d
08-28-2005, 11:10 PM
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

shibumenon
08-29-2005, 04:13 AM
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

erilaz
08-29-2005, 04:42 AM
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.

shibumenon
08-29-2005, 06:13 AM
I had never tried the "as string" method ......
thanks erilaz

Rivendale
08-29-2005, 07:39 AM
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
08-29-2005, 12:01 PM
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?

omega3d
08-29-2005, 02:01 PM
Well, now we are getting into the philosophical discussion :)

shibumenon
12-22-2005, 01:03 PM
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

f97ao
12-22-2005, 05:31 PM
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

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
12-22-2005, 06:40 PM
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
If MAXScript works the same way than C or C++ in this case (and I think it does), then you are absolutely correct. :)

CGTalk Moderation
12-22-2005, 06:40 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.