Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables tcl过程中的变量/数组_Variables_Tcl_Procedures - Fatal编程技术网

Variables tcl过程中的变量/数组

Variables tcl过程中的变量/数组,variables,tcl,procedures,Variables,Tcl,Procedures,如何在过程之外传递一些变量/数组 假设我的过程'myproc'带有输入参数{abcde},例如 myproc {a b c d e} { ... do something (calculate arrays, lists and new variables) } 在这个过程中,我想从变量a-e中计算一个数组phiN(1),phiN(2),…phiN(18),它本身就是一个列表,例如 set phiN(1) [list 1 2 3 4 5 6 7 8 9]; (假设值1-9是

如何在过程之外传递一些变量/数组

假设我的过程'myproc'带有输入参数{abcde},例如

myproc {a b c d e} { 
    ... do something
    (calculate arrays, lists and new variables)
}
在这个过程中,我想从变量a-e中计算一个数组phiN(1),phiN(2),…phiN(18),它本身就是一个列表,例如

set phiN(1) [list 1 2 3 4 5 6 7 8 9];
(假设值1-9是从输入变量a-e中计算出来的)。我想计算一些其他参数α和β

set alpha [expr a+b];
set beta  [expr c+d];
无论如何,不,我想在我的过程之外传递这些新的计算变量。与matlab相比,我只想在“函数”之外得到这些变量

[phiN,alpha,beta] = myproc{a b c d e}

有人知道我如何在tcl做生意吗??谢谢

有几个选项:

  • 返回列表并在外部使用
    例如:

    如果您返回值,但不返回数组,则这很好

  • 使用并提供数组/变量的名称作为参数
    例如:

  • 将两者结合使用
    例如:

  • 编辑:正如Donal所建议的,还可以返回dict:

    dict是一个Tcl列表,其中奇数元素是键,偶数元素是值。可以使用将数组转换为dict,也可以使用将dict转换回数组。您也可以使用它本身。
    范例


    有关更多想法(这是关于数组的,但也可以应用于值),请参阅。

    可能重复您为什么不先搜索?搜索带有“tcl”标签的“数组”和“过程”这两个词的问题会带来一些已经提问和回答的问题。感谢链接。我朝另一个方向搜索。我搜索了如何从一个PROC中传递一个以上的变量,但不特别是数组。有时候这更有意义。
    proc myproc {a b c d e} {
        set alpha [expr {$a+$b}]
        set beta [expr {$c+$d}]
        return [list $alpha $beta]
    }
    
    lassign [myproc 1 2 3 4 5] alpha beta
    
    proc myproc {phiNVar a b c d e} {
        upvar 1 $phiNVar phiN
        # Now use phiN as local variable
        set phiN(1) [list 1 2 3 4 5 6 7 8 9]
    }
    
    # Usage
    myproc foo 1 2 3 4 5
    foreach i $foo(1) {
         puts $i
    }
    
    proc myproc {phiNVar a b c d e} {
        uplevel 1 $phiNVar phiN
        set alpha [expr {$a+$b}]
        set beta [expr {$c+$d}]
        set phiN(1) [list 1 2 3 4 5 6 7 8 9]
        return [list $alpha $beta]
    }
    
    lassign [myproc bar 1 2 3 4 5] alpha beta
    foreach i $bar(1) {
         puts $i
    }
    
         proc myproc {a b c d e} {
            set alpha [expr {$a+$b}]
            set beta [expr {$c+$d}]
            set phiN(1) [list 1 2 3 4 5 6 7 8 9]
            return [list [array get phiN] $alpha $beta]
        }
    
        lassign [myproc 1 2 3 4 5] phiNDict alpha beta
        array set bar $phiNDict
        foreach i $bar(1) {
             puts $i
        }
        # Use the [dict] command to manipulate the dict directly
        puts [dict get $phiNDict 1]