这是给定的表 我需要使用tcl中的regexp在另一个文件中提取“ index_1”,“ index_2”和“ values”信息
internal_power() {
related_pin : "B0";
rise_power(energy_template_7x7) {
index_1 ("0.008, 0.04, 0.08, 0.12, 0.16, 0.224, 0.28");
index_2 ("0.01, 0.06, 0.1, 0.15, 0.2, 0.25, 0.3");
values ( \
"0.002669, 0.002702, 0.002704, 0.002706, 0.002707, 0.002707, 0.002707", \
"0.002652, 0.002695, 0.002699, 0.002701, 0.002703, 0.002703, 0.002704", \
"0.002701, 0.002737, 0.002744, 0.002748, 0.00275, 0.002751, 0.002752", \
"0.002789, 0.002814, 0.002821, 0.002823, 0.002825, 0.002826, 0.002827", \
"0.002923, 0.002914, 0.002915, 0.002919, 0.002921, 0.002922, 0.002922", \
"0.00311, 0.003083, 0.003085, 0.003085, 0.003086, 0.003087, 0.003088", \
"0.003288, 0.003249, 0.00325, 0.003265, 0.003265, 0.003264, 0.003249");
}
答案 0 :(得分:0)
您的数据非常接近有效的Tcl语法。关于Tcl的一件很酷的事情是,定义DSL非常容易:您需要覆盖默认的unknown
过程。
我敢打赌,您发布的数据只是您实际数据的一小段,因此您必须相应地进行调整。
数据文件“ data.txt”
internal_power() {
related_pin : "B0";
rise_power(energy_template_7x7) {
index_1 ("0.008, 0.04, 0.08, 0.12, 0.16, 0.224, 0.28");
index_2 ("0.01, 0.06, 0.1, 0.15, 0.2, 0.25, 0.3");
values ( \
"0.002669, 0.002702, 0.002704, 0.002706, 0.002707, 0.002707, 0.002707", \
"0.002652, 0.002695, 0.002699, 0.002701, 0.002703, 0.002703, 0.002704", \
"0.002701, 0.002737, 0.002744, 0.002748, 0.00275, 0.002751, 0.002752", \
"0.002789, 0.002814, 0.002821, 0.002823, 0.002825, 0.002826, 0.002827", \
"0.002923, 0.002914, 0.002915, 0.002919, 0.002921, 0.002922, 0.002922", \
"0.00311, 0.003083, 0.003085, 0.003085, 0.003086, 0.003087, 0.003088", \
"0.003288, 0.003249, 0.00325, 0.003265, 0.003265, 0.003264, 0.003249");
}
}
Tcl脚本“ data.tcl”
rename unknown _orig_unknown
proc unknown {cmd args} {
switch -glob -- $cmd {
internal_power* - rise_power* {
# handle "extra characters after close-quote" error
set code [regsub -all {("[^"]*")(\S)} [lindex $args 0] {\1 \2}] ; # " appease syntax highlighter
eval $code
}
related_pin {}
index_1 - index_2 - values {
dict set ::data $cmd [regexp -inline -all {[\d.-]+} [join $args]]
}
default {_orig_unknown $cmd {*}$args}
}
}
set data [dict create]
source data.txt ; # execute the txt file as code.
dict for {cmd values} $data {
puts "for $cmd, I have [llength $values] values"
puts " $values"
}
执行
$ tclsh data.tcl
for index_1, I have 7 values
0.008 0.04 0.08 0.12 0.16 0.224 0.28
for index_2, I have 7 values
0.01 0.06 0.1 0.15 0.2 0.25 0.3
for values, I have 49 values
0.002669 0.002702 0.002704 0.002706 0.002707 0.002707 0.002707 0.002652 0.002695 0.002699 0.002701 0.002703 0.002703 0.002704 0.002701 0.002737 0.002744 0.002748 0.00275 0.002751 0.002752 0.002789 0.002814 0.002821 0.002823 0.002825 0.002826 0.002827 0.002923 0.002914 0.002915 0.002919 0.002921 0.002922 0.002922 0.00311 0.003083 0.003085 0.003085 0.003086 0.003087 0.003088 0.003288 0.003249 0.00325 0.003265 0.003265 0.003264 0.003249
当然,对于eval
,您必须注意数据文件中是否存在恶意代码。