我如何 return 来自 TCL Proc 的列表?

How do I return a list from a TCL Proc?

我有以下代码-

#Create a list, call it 'p'
set p {dut.m0.x,dut.m1.y,dut.m2.z,dut.m3.z,dut.a0.vout}

#Here is a procedure to return this list 
proc get_ie_conn_ports {ie} {
        global p
        set my_list {}
        foreach n [split $p ","] {
        lappend my_list $n
    }
    return [list $my_list]
}

#This procedure call the previous procedure to retrieve the list
#It then prints it
proc print_ports_and_direction {ie} {

    set ie_ports [get_ie_conn_ports ie]
    puts $ie_ports
    foreach n [split $ie_ports (|\{|\})] {
        puts [string trim $n]
    }


}

#I call the procedure, dont worry about the argument (place holder for now)
print_ports_and_direction "dut.net00.RL_Bidir_ddiscrete_1.8"

打印此列表时,我得到了 -

dut.m0.x dut.m1.y dut.m2.z dut.m3.z dut.a0.vout

白色space不在计算范围内。请告知我如何在新行上打印每个成员。感谢您的帮助!

ie_ports 的值为 dut.m0.x dut.m1.y dut.m2.z dut.m3.z dut.a0.vout 并且您正试图拆分 ( | { } ) 中的任何一个字符,这些字符不存在于 ie_ports 中,因此您将留下整个列表。

我不确定你到底想做什么,但你可以迭代列表本身:

foreach n $ie_ports {
    puts [string trim $n]
}

另一个问题是您的过程 get_ie_conn_ports 将列表 $my_list 包装在另一个列表中,这是不需要的。您应该 return 列表本身:

proc get_ie_conn_ports {ie} {
    global p
    set my_list {}
    foreach n [split $p ","] {
        lappend my_list $n
    }
    return $my_list
}

您可能还想更改以下行:

set ie_ports [get_ie_conn_ports ie]

set ie_ports [get_ie_conn_ports $ie]

运行 对 codepad 中代码的修改给出了以下结果,其中每个成员都在一条线上:

dut.m0.x
dut.m1.y
dut.m2.z
dut.m3.z
dut.a0.vout