如何从TCL Proc返回列表?

时间:2019-03-15 16:21:36

标签: tcl

我有以下代码-

if (dateTime1.GetValueOrDefault() < dateTime2)
  ...

打印此列表时,我会收到-

#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"

没有考虑空格。请告知我如何在新行上打印每个成员。感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

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包装在另一个列表中,这是不需要的。您应该返回列表本身:

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
相关问题