1

I've string like this a sample one what I need is parse through the string and get the value of a parameter for example value of -type is Eth, so just the string next to the string I give.

card-1-3-1 3 -Number 2 -type Eth -config Yes -GEPorts 3

1 Answer 1

2
set s {card-1-3-1 3 -Number 2 -type Eth -config Yes -GEPorts 3}
dict get $s -type
# -> Eth

By default, card-1-3-1 becomes a key in the "eyes" of the dict command, but as long as that isn't a problem you don't need anything more complex than this. All the keys must have following values (i.e. the string must be a proper, even-sized list where the items are in key-value order).

Documentation: dict, set

Sign up to request clarification or add additional context in comments.

4 Comments

@user3126850: you might be using an obsolete version of Tcl. The dict command was added in Tcl 8.5, back in 2007. If you are using an older version, please upgrade: Tcl 8.4 and older are no longer supported for either security or bugs. If you can't upgrade, there is a pure-Tcl replacement for dict that can be used in Tcl 8.4.
The pure Tcl version of dict doesn't have quite all the good properties of the 8.5 one, but it might be good enough. Though upgrade is the official message.
How to handle if the key (-type) is not present ?
@Manoj: sorry, I'm having wetware problems and am rarely here. The dict get command by default errors out: you can catch that or use dict exists to look for the key beforehand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.