I am trying to split some list elements in the list.
I want to make lists from:
beforelist: {{aa, bb, cc, dd, ee;} {ff, gg, hh, ii, jj;}}
to:
afterlist: {aa bb cc dd ee ff gg hh ii jj}
I tried to deal with them by using split command, but beforelist
has some tricky point: comma, semicolon.
CodePudding user response:
A couple of ways:
#!/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}"