6

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?

Narek
  • 38,779
  • 79
  • 233
  • 389

5 Answers5

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

See this: Splitting a String Into Words.

Narek
  • 38,779
  • 79
  • 233
  • 389
ba__friend
  • 5,783
  • 2
  • 27
  • 20
  • Wow, I didn't know `regexp` could return a list. I would have done this, which is almost as good `[split [regsub { {2,}} $string " "] " "]`. The regsub replaces all sequences of spaces of length 2 or more with a single space, then the split splits on that. – drysdam Apr 20 '11 at 11:31
  • set wordList [regexp -inline -all -- {\S+} $text] here what does "--" mean? – Narek Apr 20 '11 at 11:37
  • 3
    That's necessary in case the regex begins with a "-" character, plus it's just a good habit to get into (for this plus many Tcl commands where the first non-switch argument can be perhaps user-input: `file delete -- $file`, `switch -exact -- $word`, ...) – glenn jackman Apr 20 '11 at 15:23
  • BTW see http://stackoverflow.com/questions/3369458/how-do-i-extract-all-matches-with-a-tcl-regex and http://www.hume.com/html84/mann/regexp.html for -all and -inline flags. – Narek Jun 24 '11 at 04:30
7

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}
Scott
  • 664
  • 1
  • 5
  • 8
  • Why does it work? As I know join - Create a string by joining together list elements, but your output is a list? and your input not a list... It is strange... – Narek Apr 20 '11 at 13:33
  • String, list, its all the same thing (technically). The output that was given, can easily be split into separate list elements, etc. You want your output to be a list though, don't you? – Scott Apr 20 '11 at 13:56
  • 6
    This works because the input can be parsed as a valid list. Had the input been "AAA }B C DFG 142 56", it would fail because that can't be parsed as a list. – RHSeeger Apr 20 '11 at 14:13
5

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]
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
2

As of Tcl 8.5, the following also works:

list {*}$str

(provided the string is also a proper list, as in the question). The output is the desired list.

Documentation: list, {*} (syntax)

Peter Lewerin
  • 13,140
  • 1
  • 24
  • 27
1

I know this is old, but in case others come across this in the future I'll add my solution. I subbed the unknown number of whitespace into one character of whitespace to allow split to work correctly, similar to Scott's answer:

set str "AAA    B C     DFG 142               56"    
regsub -all { +} $str " " str ; # str is now "AAA B C DFG 142 56"
set splitout [split $str { +}] ; # Split on one or more spaces. 
Ryan
  • 11
  • 6