Tcl calling numbered variable

前端 未结 1 572
抹茶落季
抹茶落季 2021-01-26 05:44

In Tcl, I assigned values to numbered variables in a loop. how can I call these variables in another loop

for {set colNum 1} {$colNum < 37} {incr colNum} {
          


        
相关标签:
1条回答
  • 2021-01-26 06:08

    If they are in the same namespace, then you can use set in this way:

    for {set colNum 1} {$colNum < 37} {incr colNum} {
        set Col$colNum 0
    }   
    for {set colNum 1} {$colNum < 37} {incr colNum} {
        puts [set Col$colNum]
    }
    

    Usually though, you may want to avoid doing it that way and use arrays instead:

    for {set colNum 1} {$colNum < 37} {incr colNum} {
        set Col($colNum) 0
    }   
    for {set colNum 1} {$colNum < 37} {incr colNum} {
        puts $Col($colNum)
    }
    

    Or use upvar to create an alias (I'm using upvar to the global namespace, #0, in the below example):

    for {set colNum 1} {$colNum < 37} {incr colNum} {
        set Col$colNum 0
    }   
    for {set colNum 1} {$colNum < 37} {incr colNum} {
        upvar #0 Col$colNum currentCol
        puts $currentCol
    }
    
    0 讨论(0)
提交回复
热议问题