Version 0 of Split On Whitespace

Updated 2018-06-07 04:36:08 by CecilWesterhof

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.