Simple Loop question

Merula_20758
Merula_20758 Altair Community Member
edited October 2020 in Community Q&A

Hey guys,

 

I declared a bunch of points using variables for the coordinates.

 

variable x1,y1,z1, x2,y,2,z2, .... in sum 20 points

 

Now I want to create notdes, but my statement does not seem to work:

 

for {set i 1} {$i < 20} {incr i} {
*createnode $[eval x$i] $[eval 'y'$i] $[eval 'z'$i] 0 0 0
}

 

So I want to read x1 and 1 is i , which gets incremented. I think it is just a simple tcl syntax error, but I am not getting the solution. Tried append, eval, expr... none of them lets me call $x1.

 

Thanks and best regards image/emoticons/default_smile.png' alt=':)' srcset='/emoticons/smile@2x.png 2x' width='20' height='20'>

Merula

Answers

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited October 2015

    If you can save coordinates into list, maybe this code can help:


    set xx {1 2 3 4 5}
    set yy {11 12 13 14 15}
    set zz {21 22 23 24 25}

    foreach x $xx y $yy z $zz {
    puts '$x $y $z'
    #do something with x y z
    }

  • Merula_20758
    Merula_20758 Altair Community Member
    edited October 2015

    Hey,

     

    thanks for the tip. It is working with plain numbers. But as I try to:

     

    set xx {$x1 $x2 $x3 $x4....}

     

    it interprets those as strings. Does not read whats in x1 2 3 ...

    So unfotunately no luck

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited October 2015

    Try this:


    # Data
    set x1 1; set x2 2; set x3 3; set x4 4; set x5 5;
    set y1 11; set y2 12; set y3 13; set y4 14; set y5 15;
    set z1 21; set z2 22; set z3 23; set z4 24; set z5 25;
    # List
    set xx [list $x1 $x2 $x3 $x4 $x5]
    set yy [list $y1 $y2 $y3 $y4 $y5]
    set zz [list $z1 $z2 $z3 $z4 $z5]
    # Loop
    foreach x $xx y $yy z $zz {
    puts '$x $y $z'
    #do something with x y z
    }

  • Merula_20758
    Merula_20758 Altair Community Member
    edited October 2015

    Success!

    Thanks!