Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a string based on lengths??

Status
Not open for further replies.

Ati2ude

Programmer
Dec 11, 2001
79
US
I would like to parse this string into mulitple strings or a list.

set string "MYSTRINGTOPARSE"

first = "MY"
second = "STRING"
third = "TO"
fourth = "PARSE"
etc...


Any pointers would be great, as always thanks in advance.

Brian

 
If you know the lengths you can use regexp as follows:

regexp {(\w{2})(\w{6})(\w{2})(\w{5})} $string match first second third forth


_________________
Bob Rashkin
rrashkin@csc.com
 
Like BONG suggested, in your example you must know the lengths of the expected string since you have no delimitors in your initial string.

That said, let's assume you do have some kind of a delimitor (e.g. - " ")....

set string "MY STRING TO PARSE"
set n 0

### Set-up a variable array - va(1), va(2), etc.
foreach x $string {
set va($n) $x
incr n
}

### display each value of the array
for {set i 0} {$i < $n} {incr i} {
puts $va($i)
}

### and just to demostrate the names of array
puts [array names va]


OUTPUT:
my
string
to
parse
0 1 2 3

...hope that helps!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top