Reputation: 3
Is it possible to convert items of a string into a separate list for each item? set a [list "1.2 1.3 1.6 1.7 1.8"]
and to have an output like {1.2}{1.3}{1.6}{1.7}{1.8}
Upvotes: 0
Views: 236
Reputation: 393
getting list from a string. We are using whitespace " " as separator of list elements
set a [split "1.2 1.3 1.6 1.7 1.8" " "]
printing
foreach one $a {
puts -nonewline "{$one}"
}
puts ""
Upvotes: 0
Reputation: 137567
We can use a regular expression to parse the string, and join
to make something with the result:
set a "1.2 1.3 1.6 1.7 1.8"
set b [regexp -all -inline {\S+} $a]
set c "{[join $b "}{"]}"
Now, if we knew we had a proper list, we'd be able to skip the regular expression stuff, but it is safest to not do that.
We could also use regsub
to do the transformation in this case.
set c [regsub -all {\s*(\S+)\s*} $a {{\1}}]
However, writing this sort of transformation can get quite a bit more difficult once the transformation required gets more complex.
Upvotes: 1