add a NEW value in a list

407 Views Asked by At

I have a problem with this VEX script. @ create attributes instead of variables.

f[]@myList; // create list attribut
float pavW = 0.0;
for( int i = 1 ; i < 11 ; i++){
    string path = "../widthPav" + itoa(i);
    pavW = ch(path); // take the value in the specific channel
    if (i == 1){
        push(@myList,pavW); //add new component of value to myList
    }
    if ( pavW > 0){
        foreach ( int j ; float value ; @myList){
            if ( pavW == value){
                break;
            }else{
                push(@myList,pavW); //add new component...
                break;
            }
        }
    }

}

I want to add pavW value to myList if pavW is different than the elements of myList. The result is not as expected.

1

There are 1 best solutions below

0
PradeepBarua On

In foreach you are comparing to the first element of array only. And for other elements your if conditions fails and keeps on adding to the myList. You should push to the array outside foreach block.

A temporary var named unique can be used to trigger the push.

f[]@myList; // create list attribut
float pavW = 0.0;
for (int i = 1; i < 11; i++)
{
    string path = "widthPav" + itoa(i);
    pavW = ch(path); // take the value in the specific channel
    if (i == 1)
    {
        push(@myList, pavW); //add new component of value to myList
    }
    if (pavW > 0)
    {
        int unique = 0;
        foreach (float value; @myList)
        {
            unique = 1;
            if (pavW == value)
            {
                unique = 0;
                break;
            }
        }
        if (unique)
            push(@myList, pavW); //add new component...
    }
}

Suggestions:

  • You don't need index in foreach.
  • For this kind of task, simply create an spare parm and add a Python expression to get list of unique widthPav parm.

Simple Python snippet:

node = hou.pwd()
parmsUniqueValue = []

[parmsUniqueValue.append(parm.eval()) for parm in node.parms() if parm.name().startswith('widthPav') if parm.eval() not in parmsUniqueValue]