Kaushik Koneru
Kaushik Koneru

Reputation: 99

Parse through string in TCL script

I need to parse through the array and find out a value at particular place in a TCL script

E.g., I have a string

set var "00 01 02 03"

I need to parse through the var to find what is there in the 3rd entry (02).

Upvotes: 2

Views: 2492

Answers (2)

bmk
bmk

Reputation: 14137

Your string can be interpreted as a list, so you could use lindex to get the 3rd list element (counted starting with index 0):

lindex $var 2

Better would be (and works with different delimiters, too):

lindex [split $var " "] 2

Upvotes: 4

TrojanName
TrojanName

Reputation: 5355

What you need is a TCL list. Remember the index counter starts at 0, so pass in 2 to lindex to find the 3rd element

% set my_list [list 00 01 02 03]
00 01 02 03
% lindex $my_list 2
02

Upvotes: 4

Related Questions