Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCL - split string by arbitrary number of whitespaces to a list

Say I have a string like this:

set str "AAA    B C     DFG 142               56"

Now I want to get a list as follows:

{AAA B C DFG 142 56}

For that I want to use split function, but in that case I get some extra empty lists {}. How I can get the list above?

like image 793
Narek Avatar asked Apr 20 '11 10:04

Narek


3 Answers

set text "Some arbitrary text which might include \$ or {"
set wordList [regexp -inline -all -- {\S+} $text]

See this: Splitting a String Into Words.

like image 186
ba__friend Avatar answered Oct 15 '22 03:10

ba__friend


You can always do the following:

set str "AAA    B C     DFG 142               56"
set newStr [join $str " "]

It will output the following:

{AAA B C DFG 142 56}
like image 7
Scott Avatar answered Oct 15 '22 05:10

Scott


The textutil::split module from tcllib has a splitx proc that does exactly what you want

package require textutil::split
set result [textutil::split::splitx $str]
like image 5
glenn jackman Avatar answered Oct 15 '22 04:10

glenn jackman