Arrays 如何使用TCL从proc返回值

Arrays 如何使用TCL从proc返回值,arrays,list,return,tcl,proc,Arrays,List,Return,Tcl,Proc,我有一个样本程序 proc exam {return_value} { set input "This is my world" regexp {(This) (is) (my) (world)} $input all a b c d set x "$a $b $c $d" return x } 在上面的过程执行之后,我将在单个列表中获得所有的b c d值,因此如果我只想从上面的过程中获得b值,现在我正在执行[li

我有一个样本程序

      proc exam {return_value} {

        set input "This is my world" 
         regexp {(This) (is) (my) (world)} $input all a b c d 
         set x "$a $b $c $d" 
    return x }
在上面的过程执行之后,我将在单个列表中获得所有的b c d值,因此如果我只想从上面的过程中获得b值,现在我正在执行[lindex[exam]1]。
我正在寻找以不同方式获得输出的其他方法,而不是使用lindex或returun_值(b)可以给出我的预期输出

您可以使用
dict
并选择这样的键值映射,以明确您的意图:

return [dict create width 10 height 200 depth 8]

我认为在Tcl中,除了复合数据结构或从
corroutine

返回多个值之外,没有其他方法可以返回多个值。这可以在调用站点与
lassign
一起使用,以便列表立即分解为多个变量

proc exam {args} {
    set input "This is my world" 
    regexp {(This) (is) (my) (world)} $input all a b c d 
    set x "$a $b $c $d" 
    return $x
}

lassign [exam ...] p d q bach
您还可以返回字典。在这种情况下,
dict with
是一种方便的解包方式:

proc exam {args} {
    set input "This is my world" 
    regexp {(This) (is) (my) (world)} $input all a b c d 
    return [dict create a $a b $b c $c d $d]
}

set result [exam ...]
dict with result {}
# Now just use $a, $b, $c and $d
最后,您还可以在
exam
中使用
upvar
将调用者的变量引入范围,尽管通常最明智的做法是只使用调用者提供给您名称的变量

proc exam {return_var} {
    upvar 1 $return_var var
    set input "This is my world" 
    regexp {(This) (is) (my) (world)} $input all a b c d 
    set var "$a $b $c $d" 
    return
}

exam myResults
puts "the third element is now [lindex $myResults 2]"

请澄清你的问题。你到底在找什么?你认为你应该做什么才能得到你想要的?类似于
[exam b]
只返回
$b
[exam d]
只返回
$d
可能或
[exam ab]
返回的列表是
$a$b
?不@jerry这不是故意的,我期待的是“[exam first_run]”。在本例中,在proc执行之后,如果我想获取存储在b中的值。例如,如果我执行“put$1st_run(b)”,它应该有“is”感谢您的帮助这里使用upvar与其他语言中的pass-by-reference意思相同吗?