Version 1 of Split On Whitespace

Updated 2018-06-07 06:46:36 by StephanKuhagen

Created by CecilWesterhof.

Often I want to split a string on repeating white-space. The normal split function does not do what I want. For example:

split "   To   show    the   problem.   "

gives:

{} {} {} To {} {} show {} {} {} the {} {} problem. {} {} {}

That is why I created the following proc:

proc splitOnWhiteSpace {value {count -1}} {
    set splitLst [regexp -all -inline {\S+} ${value}]
    if {${count} != -1} {
        set length [llength ${splitLst}]
        if {${length} != ${count}} {
            error "'${value}' contains ${length} instead of ${count} values"
        }
    }
    return ${splitLst}
}

With this I get:

To show the problem.

Beside splitting on repeating white-space, it can also check the count. For example:

splitOnWhiteSpace "Just a test." 4

gives:

'Just a test.' contains 3 instead of 4 values

As always: comments, tips and questions are appreciated.


StephanKuhagen:

About four times faster compared to the regexp-line:

list {*}[string map {\{ \\\{} $value]

The string map is needed to avoid unmatched open braces in lists. If you know, that there will never be an opening brace in your inputs, you can get it even faster.