假设我输入了像AAAABBCCC .....
这样的模式然后脚本应该像T1 0 A-4 B-2 C-3 0 T1
那样打印其中T1为tap,0表示下一行......
如果patern就像AAABBCC CABCCBA
然后脚本应该生成模式T1 0 A-3 B-2 C-2 0 T1 0 C A B C-2 B A 0 T1#应从T1开始,以T1 ....结束,两个模式之间只允许一个T1 ...... 我是TCL的新手,请帮助.....
答案 0 :(得分:3)
set pattern1 {AAAABBCCC}
set pattern2 {AAABBCC
CABCCBA}
proc process {pattern} {
lappend result "T1"
foreach line [split $pattern "\n"] {
lappend result "0"
foreach {chunk symbol} [regexp -all -inline {(.)(?:\1+)?} $line] {
set num [string length $chunk]
lappend result $symbol[expr {($num>1)?"-$num":""}]
}
lappend result "0" "T1" "\n"
}
return [join $result]
}
puts [process $pattern1]
puts [process $pattern2]
上面代码的输出:
T1 0 A-4 B-2 C-3 0 T1
T1 0 A-3 B-2 C-2 0 T1
0 C A B C-2 B A 0 T1