split on spaces - TCL

This is a discussion on split on spaces - TCL ; Hi there, I am trying to split a text line on spaces but managed to do it only on ":" with the following line set line_elements [split $line :] how should I change it to split on spaces? Thanks Ivan...

+ Reply to Thread
Results 1 to 7 of 7

split on spaces

  1. Default split on spaces

    Hi there,

    I am trying to split a text line on spaces but managed to do it only on ":"
    with the following line

    set line_elements [split $line :]

    how should I change it to split on spaces?

    Thanks
    Ivan



  2. Default Re: split on spaces

    Hi Ivan

    try

    set line_elements [split $line { }]

    or use

    set line_elements [split $line " \n\r\t"]


    to split on most white spaces (spaces, tabs, carriage return, line feed)


    Ivan schrieb:
    > Hi there,
    >
    > I am trying to split a text line on spaces but managed to do it only on ":"
    > with the following line
    >
    > set line_elements [split $line :]
    >
    > how should I change it to split on spaces?
    >
    > Thanks
    > Ivan
    >
    >


  3. Default Re: split on spaces

    On 26 Sep., 17:27, "Ivan" <idipr...@hotmail.com> wrote:
    > Hi there,
    >
    > I am trying to split a text line on spaces but managed to do it only on ":"
    > with the following line
    >
    > set line_elements [split $line :]
    >
    > how should I change it to split on spaces?


    % set data "these are fields separated by spaces"
    these are fields separated by spaces
    % split $data
    these are fields separated by spaces
    % split $data " "
    these are fields separated by spaces


  4. Default Re: split on spaces

    On Wed, 26 Sep 2007 16:27:14 +0100,
    Ivan <idiprima@hotmail.com> wrote:

    >I am trying to split a text line on spaces but managed to do it only on ":"
    >with the following line
    >
    >set line_elements [split $line :]
    >
    >how should I change it to split on spaces?


    Split on space character:
    split $line " "

    Split on any whitespace character:
    split $line

    Split on any *group* of one or more whitespace character...
    this is a bit harder, but here's my usual solution -
    I'm sure someone else can offer something nicer:

    regsub -all {\s+} $line { } line
    set elements [split $line]

    --
    Jonathan Bromley, Consultant

    DOULOS - Developing Design Know-how
    VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

    Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
    jonathan.bromley@MYCOMPANY.com
    http://www.MYCOMPANY.com

    The contents of this message may contain personal views which
    are not the views of Doulos Ltd., unless specifically stated.

  5. Default Re: split on spaces

    On 2007-09-26, Jonathan Bromley <jonathan.bromley@MYCOMPANY.com> wrote:
    > On Wed, 26 Sep 2007 16:27:14 +0100,
    > Ivan <idiprima@hotmail.com> wrote:
    >
    >>I am trying to split a text line on spaces but managed to do it only on ":"
    >>with the following line
    >>
    >>set line_elements [split $line :]
    >>
    >>how should I change it to split on spaces?

    >
    > Split on space character:
    > split $line " "
    >
    > Split on any whitespace character:
    > split $line
    >
    > Split on any *group* of one or more whitespace character...
    > this is a bit harder, but here's my usual solution -
    > I'm sure someone else can offer something nicer:
    >
    > regsub -all {\s+} $line { } line
    > set elements [split $line]


    There's this:

    set elements [regexp -all -inline {\S+} $line]

    although it does have the side-effect of trimming leading and
    trailing white-space.

    --
    Az.

    www: http://www.azazel.net/
    pgp: http://www.azazel.net/~azazel/az_key.asc

  6. Default Re: split on spaces


    "Stefan Finzel" <Stefan.G.R.Finzel@T-Online.de> wrote in message
    news:46FA7C6E.7040608@T-Online.de...
    > Hi Ivan
    >
    > try
    >
    > set line_elements [split $line { }]
    >
    > or use
    >
    > set line_elements [split $line " \n\r\t"]
    >
    >
    > to split on most white spaces (spaces, tabs, carriage return, line feed)
    >

    hi Stefan,

    sorry but they both dont work... here is how I loop through the lines into
    the file:

    foreach line $file_list {

    if {[string length $line] > 0} {

    set line_elements [split $line :]
    ....
    }
    }

    any ideas?




  7. Default Re: split on spaces

    Ivan wrote:
    > "Stefan Finzel" <Stefan.G.R.Finzel@T-Online.de> wrote in message
    > news:46FA7C6E.7040608@T-Online.de...
    >> Hi Ivan
    >>
    >> try
    >>
    >> set line_elements [split $line { }]
    >>
    >> or use
    >>
    >> set line_elements [split $line " \n\r\t"]
    >>
    >>
    >> to split on most white spaces (spaces, tabs, carriage return, line feed)
    >>

    > hi Stefan,
    >
    > sorry but they both dont work... here is how I loop through the lines into
    > the file:
    >
    > foreach line $file_list {
    >
    > if {[string length $line] > 0} {
    >
    > set line_elements [split $line :]
    > ...
    > }
    > }
    >
    > any ideas?


    Can you define what "dont work" means to you? Maybe your expectation
    isn't the same as how it is documented to work. Can you show what you
    are getting and how it differs from what you are expecting?



    --
    Bryan Oakley
    http://www.tclscripting.com

+ Reply to Thread

Similar Threads

  1. replacing spaces
    By Application Development in forum RUBY
    Replies: 2
    Last Post: 11-19-2007, 08:22 AM
  2. Did you know about spaces?
    By Application Development in forum Eudora
    Replies: 12
    Last Post: 09-06-2007, 09:59 PM
  3. How do I split-screen so the split is blurred?
    By Application Development in forum Adobe Premiere
    Replies: 2
    Last Post: 07-23-2005, 02:18 PM
  4. checking for spaces
    By Application Development in forum basic.visual
    Replies: 7
    Last Post: 10-08-2004, 07:43 PM
  5. How to use split on spaces AND tabs?
    By Application Development in forum Perl
    Replies: 2
    Last Post: 07-03-2004, 04:24 PM