[Closed] resetting arrays
Hi guys,
Doing my head over this one …
try this
p_group = #()
p_group != #()
gives a value of true!
whats going on
Just a bit more explanation…
I’m dealing with an array that originally held a list of objects imported from a text file.
If a new lot of objects get imported I wanted a test to see if the original empty array was still empty or held a list. So I thought lets use
p_group != #()
to see if its empty or not.
already had lots of fun emptying the array of deleted scene nodes etc…hehe, but sorted that out now. Just dont understand why it returns a true value when it should be false
That IS strange… not got MAX in front of me so I can’t try it bu you could try p_group.count != 0 maybe
From what i know you cannot do that. For example
var1=#(1,2,3)
var1!=#(1,2,3)
as you say will give true.
Or let’s take
var1=#(1,2,3)
var2=#(1,2,3)
var1==var2
will give false(since arrays use reference assignment)
So, you cannot compare arrays with == or != operator
Alex,
It might be that in the comparison case, that the variable is literally being compared to the literal array value instead of what the variable actually references (an array value).
It might be something special with array values because I just tried the same scenario with a number value and it doesn’t do that.
This is what I got in the listener:
b=5
5
b!=5
false
It worked with a string value too… I guess it’s special with array values.
It’s different because it uses reference assignment.
Try with this
var1=#(1,2,3)
var2=var1
var1==var2
It will give true because they point to the same direction
If you then do this
var2[1]=5
and then see the values of var 1 you will see
var1==#(5, 2, 3)
Just so I get this straight…
If I have an array say
p_group = #()
then I can’t do p_group != #()
but I could do
temp = #()
then
temp == p_group
or
p_group [x] != undefined
as a test
About reference assignment:
There is a chapter in maxscript help that explains it(search reference assignment). In reference assignment the variable contains a reference(a memory direction) where the data/value is stored. If you have
var1=#(1,2,3)
and you do var2=var1
Assigning var1 value to var2, the reference to that value’s memory is placed in var2
writing in the listener, you get #(1,2,3).
If you change the data then
var1[2]=4
and write again var2 you get #(1,4,3) because you have a reference to the data, not a copy of it.
By the way, when you pass pararmeters to a function you use value assignment(the function gets a copy of them and it is destroyed when finished), but if you pass an array to a function, the function gets a reference of it. So, if you alter it inside the function, outside it it gets updated.
p_group = #()
then I can’t do p_group != #()
You are comparing references of a var with and empty array, not values
temp = #()
temp == p_group
You are comparing the direction to which temp and p_group references. If they are the same the result will be true. In this case they are diferent, so it’s false.
If you make
p_group=#(1,2,3)
temp=#(1,2,3)
temp==p_group
it will give false since you are comparing the references of the variables, not the values.
And if finally you do this
p_group=#(1,2,3)
temp=p_group
temp==p_group
It will give true
And
p_group [x] != undefined
Here you are comparing the value of the index x of the array
p_group=#(1,undefined,3)
p_group[1]!=undefined
p_group[2]!=undefined
p_group[3]!=undefined
p_group[1] is 1. 1!=undefined ? YES, so true
p_group[2] is undefined. undefined!=undefined ? NO, so false
p_group[3] is 3. 3!=undefined ? YES, so true
Sorry about crappy english 8)