我想将标题vdd,vddpll,..作为键和关联的列条目存储为值。 从下面的示例中,想要读取每列并存储列中的第一个条目。
vdd -> ON ON OFF OFF vddpll -> ON OFF ON OFFF
...等
create_pst top_pst -supplies {vdd vddpll vdd_NWELL vdd33 gnd gnd_PWELL} add_pst_state state1 -pst top_pst -state {ON ON ON ON gnd_on gnd_on} add_pst_state state2 -pst top_pst -state {ON OFF ON ON gnd_on gnd_on} add_pst_state state3 -pst top_pst -state {OFF ON ON ON gnd_on gnd_on} add_pst_state state4 -pst top_pst -state {OFF OFF ON ON gnd_on gnd_on}
set fp [open "test.upf" r]
set i 0
while {[gets $fp data] >= 0} {
if {[regexp "create_pst" $data match]} {
set supplies_row [split_line $data]
set supply_names [lrange $supplies_row 3 end]
for {set j 0} {$j < [llength $supply_names]} {incr j} {
set power_name [lindex $supply_names $j]
set power_and_state($power_name) {}
}
if {[regexp "add_pst_state" $data match]} {
set state_row [split_line $data]
set supply_state [lrange $state_row 5 end]
for {set j 0} {$j < [llength $supply_state]} {incr j} {
set power_name [lindex $supply_names $j]
lappend power_and_state($power_name) [lindex $supply_state $j]
}
}
proc split_line {data} {
set upf_entries1 [regexp -all -inline {\S+} $data]
set entries1 [regsub "{" $upf_entries1 ""]
set entries2 [regsub "}" $entries1 ""]
set entries3 [regsub -all {\-} $entries2 ""]
set entries4 [regsub -all {\\} $entries3 ""]
return $entries4
}
}
close $fp
我希望power_and_state数组包含所有键vdd,vddpll..etc,相关的条目存储在分配给键的列表中。
提前感谢您的帮助。
答案 0 :(得分:4)
我期待power_and_state数组包含所有键vdd, vddpll..etc和相关条目存储在分配的列表中 到钥匙。
我认为你的“数据提要”非常符合Tcl的原生语法,所以不需要通过正则表达式绕道而行。尽可能地捎带到Tcl上。一种直截了当的方式如下:
<body>
<div class=abspos>
<input type=checkbox>
<div class=t><div>asd</div></div>
<div class=e>fasd<br>lova</div>
</div>
</body>
set data {create_pst top_pst -supplies {vdd vddpll vdd_NWELL vdd33 gnd gnd_PWELL}
add_pst_state state1 -pst top_pst -state {ON ON ON ON gnd_on gnd_on}
add_pst_state state2 -pst top_pst -state {ON OFF ON ON gnd_on gnd_on}
add_pst_state state3 -pst top_pst -state {OFF ON ON ON gnd_on gnd_on}
add_pst_state state4 -pst top_pst -state {OFF OFF ON ON gnd_on gnd_on}}
proc create_pst {name args} {
global $name
set opts [dict create {*}$args]
set $name [dict create {*}"[join [dict get $opts -supplies] " {} "] {}"]
return
}
proc add_pst_state {name args} {
set opts [dict create {*}$args]
set pst [dict get $opts -pst]
global $pst
set state [dict get $opts -state]
foreach k [dict keys [set $pst]] v $state {
dict lappend $pst $k $v
}
return
}
eval $data
puts $top_pst
和create_pst
匹配“输入声明”。add_pst_state
第一个arg命名的全局dict
。create_pst
会使用所有密钥初始化全局create_pst
,以保留其订单。dict
)来调用保留订单的add_pst_state
。foreach
,主要用于处理两个过程的非位置参数(dict
,-supplies
,-pst
)。