PDA

View Full Version : Testing if array1 != aray2?


Gibbz
11-10-2005, 09:14 AM
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

)

prettyPixel
11-10-2005, 10:15 AM
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

PEN
11-10-2005, 10:19 AM
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.

Gibbz
11-10-2005, 10:22 AM
okay thought i would have to do something like that.

Thanks :D

PEN
11-10-2005, 10:22 AM
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

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

prettyPixel
11-10-2005, 10:26 AM
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
11-10-2005, 10:26 AM
In reading your post again I'm not sure what that does for you??

PEN
11-10-2005, 10:29 AM
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] then return false )
true
)

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

prettyPixel
11-10-2005, 10:34 AM
Oh yes... If the arrays to compare contain one array themselves my function does not work.
Thanks for the idea of recursivity, Pen.

PEN
11-10-2005, 05:27 PM
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
*/

prettyPixel
11-10-2005, 06:07 PM
Hey you did it!
Thanks :thumbsup:


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=% \n" 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?

Zbuffer
11-10-2005, 08:09 PM
Hi !


fn test a b c:333 =
(
format "a=% b=% c=% \n" 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


you must explicitely supply the c variable when calling the function:
test 111 222 c:999
-> a=111 b=222 c=999


this enables you to override variables used in functions

FaceMn
11-10-2005, 08:16 PM
Shorter version:

fn compare a b &bool = (
if a.count != b.count do (
bool = false
)

for i = 1 to a.count do (
-- Test for nested arrays
if classof a[i] == Array and classof b[i] == Array then (
compare a[i] b[i] &bool
) else (
-- Test for strings
if classof a[i] == String and classof b[i] == String do (
if (stricmp a[i] b[i]) != 0 then (
bool = false
) else (
continue
)
)

if a[i] != b[i] do (
bool = false
)
)
)
)

myVal = true
compare myArr1 myArr2 &myVal
print myVal

PEN
11-11-2005, 11:19 AM
Hey you did it!
Thanks :thumbsup:


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=% \n" 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?

As was pointed out you don't use c=333 but instead you should use c:333. This is just the same as creating a box lets say, box height:50 width:20 ...

prettyPixel
11-11-2005, 11:57 AM
Thanks for the explanation, Zbuffer and 'Ogre' ;) (Paul).
That will undoubtedly be useful for me !

Thank you also for writing this smaller version, FaceMn.

Gibbz
11-13-2005, 09:58 PM
hrmm i just noticed i have an odd problem.

If i do
"print myarray.count" it prints out one of the items in the array, and not the array count(number of items).

Any Ideas?

prettyPixel
11-13-2005, 10:54 PM
Indeed it's strange. Maybe if you insert myArray.count between ( ), that will be better ? or to use 'as string'


But I prefer to use the command 'format':

myArray=#(1, 2, 3)
format "myArray.count=% \n" myArray.count

I think that the result will be more reliable.

Gibbz
11-14-2005, 07:41 AM
Hrmm that did not work either :|

Thats really freaky, is there any other way to get the count of an array?

Is this a bug? it happens on my home machine and also at work....

Max 8

prettyPixel
11-14-2005, 09:10 AM
What kind of array do you use ?
Show us an example...

PEN
11-14-2005, 12:13 PM
Ya I have to see an example that will cause this as well. Never seen this happen.

prettyPixel
11-14-2005, 12:44 PM
I am almost certain that he uses the ".count" with a bitarray and not with an array.
If it is the case, That deserves a punishment. lol :twisted:

a=#{11,22,33}
a.numberset
a.count

PEN
11-14-2005, 12:53 PM
Ah yes, I would assume so. Well deduced Watson!

Gibbz
11-14-2005, 09:17 PM
argh your right :D it is a bit array

So .count cannot be used on bitarrays?

Edit: this works ass an array :)

Thanks

CGTalk Moderation
11-14-2005, 09:17 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.