Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

split on spaces

1,550 views
Skip to first unread message

Ivan

unread,
Sep 26, 2007, 11:27:14 AM9/26/07
to
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


Stefan Finzel

unread,
Sep 26, 2007, 11:36:14 AM9/26/07
to Ivan
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:

suchenwi

unread,
Sep 26, 2007, 11:40:19 AM9/26/07
to

% 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

Jonathan Bromley

unread,
Sep 26, 2007, 11:49:05 AM9/26/07
to
On Wed, 26 Sep 2007 16:27:14 +0100,
Ivan <idip...@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...@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.

Azazel

unread,
Sep 26, 2007, 11:58:17 AM9/26/07
to
On 2007-09-26, Jonathan Bromley <jonathan...@MYCOMPANY.com> wrote:
> On Wed, 26 Sep 2007 16:27:14 +0100,
> Ivan <idip...@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

Ivan

unread,
Sep 26, 2007, 12:19:27 PM9/26/07
to

"Stefan Finzel" <Stefan.G...@T-Online.de> wrote in message
news:46FA7C6E...@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?

Bryan Oakley

unread,
Sep 26, 2007, 1:14:22 PM9/26/07
to

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

0 new messages