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