delimiter '::' with split 
Author Message
 delimiter '::' with split

Thanks to all who took the time to answer my question regarding using
arguments with unix tools such as date.  I like this list. My first
foray into it brought proof of highly civilized beings.
Got another question:

        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.

I have worked with variations and the only one that works is
replacing the '::' with ':'. Not what I wanted.

TIA,
-lloyd
--
'I eats more chicken than any man ever seen.'
        - Willie Dixon, BackDoor Man



Tue, 11 May 1999 03:00:00 GMT  
 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



Tue, 11 May 1999 03:00:00 GMT  
 delimiter '::' with split



Quote:

>    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.

Well that _is_ what the man page says it will do.  Anyway, you might something
like this, which is more like perl's split command (but unfortunately only
works with Tcl's regexps).

---------

# Split on a regular expression (like in perl).  Notice that the string
# argument comes before the exp argument, like in split, but unlike in regexp
# and regsub.  Warning: expressions that can match the emtpty string will cause
# an infinite loop.
proc rsplit {string exp} {
    set ret ""
    while {[regexp -indices -- $exp $string range]} {
        foreach {start end} $range {}
        lappend ret [string range $string 0 [expr $start-1]]
        set string [string range $string [expr $end+1] end]
    }
    lappend ret $string
    return $ret

Quote:
}

--------

Grant



Wed, 12 May 1999 03:00:00 GMT  
 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::
[snip]

> > 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.

> > 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.  

[snip]

Alternatively, there may be some character that you can guarantee isn't
in the source string, so you can do this:

        regsub -all {::} $string "\n" string
        foreach elem [split $string "\n"] {
            ...
        }

You can guarantee that "\n" isn't in the source string if it appeared as
the result of a "gets".  You can do this

        join [split $string "\n"] "::"

to get back to the original string.

--
John Haxby

------------------------------------------------------------------------
These are my opinions, not my employer's.



Sat, 22 May 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. splitting using '\' as a delimiter

2. 'split' creates extra output

3. Splitting 'and' conditions into multiple conditions

4. Splitting a string using '/n' as the delimiter

5. Behavior of 'split' command

6. newbie: emacs 'split-string'

7. Splitting a string every 'n'

8. changing read's string delimiter

9. split that includes delimiter ?

10. split on string that included delimiter

11. Splitting on a regex w/o consuming delimiter

12. Interesting tidbit: 'scan' slower than 'split'

 

 
Powered by phpBB® Forum Software