我正在嘗試拆分列表中的一些列表元素。
我想列出以下內容:
前列: {{aa, bb, cc, dd, ee;} {ff, gg, hh, ii, jj;}}
到:
後名單: {aa bb cc dd ee ff gg hh ii jj}
我試圖通過使用 split 命令來處理它們,但beforelist
有一些棘手的問題:逗號、分號。
幾種方式:
#!/usr/bin/env tclsh
set beforelist {{aa, bb, cc, dd, ee;} {ff, gg, hh, ii, jj;}}
# Iterate over each element of each list in beforelist and remove
# trailing commas and semicolons before appending to afterlist
set afterlist {}
foreach sublist $beforelist {
foreach elem $sublist {
lappend afterlist [regsub {[,;]$} $elem ""]
}
}
puts "{$afterlist}"
# Remove all commas and semicolons at the end of a word followed by space
# or close brace, then append sublists to afterlist.
set afterlist {}
foreach sublist [regsub -all {\M[,;](?=[ \}])} $beforelist ""] {
lappend afterlist {*}$sublist
}
puts "{$afterlist}"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句