Returning Some Variable from a "proc" in Tcl

95 views Asked by At

Suppose that a procedure in Tcl is as follows:

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

Now, I define this:

set IDsection {1 3}

Then, I would like to read all values (arbitrary number, 2 or more) into a set (IDsection) which would show the ID in above procedure and generate the corresponding y:

set Load {30 40}

How I do produce the values in {} in front of "Load"?

1

There are 1 answers

1
glenn jackman On BEST ANSWER

You could do it like this:

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