How to clean up the data in an array?

I'm testing my script which has some array declaration in it. It goes like this:

string $ctrlList[] = {"ctrl1", "ctrl2", "ctrl3"};

But when I try to make a change in one of the element and re-excute it in Script Editor,ex. ctrl2-->ctrl4, ctrl2 wasn't replaced by ctrl4. If I print out the array, it's like

ctrl1
ctrl2
ctrl3
ctrl1
ctrl4
ctrl3

is there something i can do to clean it up first? otherwise I have to restart maya everytime for testing it...

thax

cooki

{
string $ctrlList[];
//...etc
}

how could I not think of this. thax~

I have a new problem now....now the data is right, but seems like there are lots of empty space in the begining. so when I try to get the 1st element in the array, it returns nothing. If I print it out, it's like:

ctrl1
ctrl2
ctrl3

does anyone have the same problem?

You've probably put empty strings in the array. So it doesn't return nothing, it returns empty string. You can remove array elements using stringArrayRemove

string $result[] = stringArrayRemove( { "" }, $array );

1 Like

thanks a lot again !!

Why not
clear $ctrlList;
?

yes it make the variable 0 long but odesnt delete it. Its not somehthing you would want to use to replace scope, or in other words the "{}" block

another quick question. how do u clear and erase all the variables(arrays, data type) when u debug in script editor?

thax again

You can't delete global variables. Always use scope as described before.

does that mean if I want reassign the data type (like change it from string to string[]), I have to restart maya again?

Yes

Dont make global variables out of them. Let them die and create them anew for soome optionvars instead.

And yes this is actualy the basis of maya variables that you CANT reassign variables once they are declared they die whan their scope dies (and for global vars tahat means whan maya quits). Thats why you use names taht arent likely to reoccour for globals.