delimiter '::' with split
Quote:
> <SNIP>
> How do I use two chars such as '::' with split?
> As an example,
> read in string col1::col2::col3::col4::
> if run through
> split $string ::
> does not produce what I expected, ie:
> lindex $string 0 == col1
> lindex $string 1 == col2
> etc....
> Instead, split treats it as though the delimiter is ':'
> producing empty strings for every other $string.
"Split" treats the split delimiter parameter as a character set, any character
of which will be used as a split delimiter.
Quote:
> I have worked with variations and the only one that works is
> replacing the '::' with ':'. Not what I wanted.
Here's something I wrote some time ago to do the job. I wrote this a while
ago; rewriting using "regexp" may be faster. I'll leave that as an exercise for
the stude-net.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Pass in a string and a multi-character string you wish to split upon
# (a la Perl's "split()"), and you get back an array of elements
# determined by spliting the original input string by the split string.
#
proc StringSplit {s spl} {
if {[set ix [string first $spl $s]] == -1} { return [list $s] }
set pre [string range $s 0 [expr $ix - 1]]
set post [string range $s [expr $ix + [string length $spl]] end ]
return [concat [list $pre] [StringSplit $post $spl]]
Quote:
}
Ron A. Zajac