gpt4 book ai didi

tcl - 如何从 TCL Proc 返回列表?

转载 作者:行者123 更新时间:2023-12-04 15:47:21 24 4
gpt4 key购买 nike

我有以下代码-

#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

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

最佳答案

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

关于tcl - 如何从 TCL Proc 返回列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55186813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com