从Tcl中的“proc”返回一些变量

时间:2015-06-12 12:29:44

标签: 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}

然后,我想将所有值(任意数字,2或更多)读入一个集合(IDsection),该集合将在上述过程中显示ID并生成相应的{ {1}}:

y

我如何在“加载”前面的{}中生成值?

1 个答案:

答案 0 :(得分:0)

你可以这样做:

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