从 Tcl 中的 "proc" 返回一些变量

Returning Some Variable from a "proc" in Tcl

假设Tcl中的一个过程如下:

proc Section {ID x y} {
.
.
"Some calculations do here"
.
.
}

Section 1 20 30
Section 2 25 35
Section 3 30 40
Section 4 35 45

现在,我这样定义:

set IDsection {1 3}

然后,我想将 all 值(任意数字,2 个或更多)读取到一个集合 (IDsection) 中,这将在上述过程中显示 ID并生成对应的y:

set Load {30 40}

如何在 "Load" 前面的 {} 中生成值?

你可以这样做:

proc Section {ID x y} { 
    set ::section_data($ID) [list $x $y] 
}

proc getLoads {ids} {
    global section_data
    foreach id $ids {
        lappend loads [lindex $section_data($id) end]
    }
    return $loads
}

Section 1 20 30
Section 2 25 35
Section 3 30 40
Section 4 35 45

set IDsection {1 4 1 3}

set Load [getLoads $IDsection]   ;# => 30 45 30 40