0

I have a list in TCL:

{word0} {word1} {word2} {word3}

I need to set a new string to one of the values in the list. What would the syntax be for that?

set test {$my_list 0}
set test [$my_list 0]

Neither work.

Sorry for the new question, but I'm not familiar with this syntax and not finding it in the docs.

2
  • What would you like as result? I'm not sure I understand your question otherwise. Commented Apr 18, 2014 at 20:21
  • The result should be word0. For $my_list 1 it would be word1 and so on. Commented Apr 18, 2014 at 20:25

1 Answer 1

4

You can use lindex:

set my_list [list word0 word1 word2 word3]
set test [lindex $my_list 0]
puts $test
# => word0

In Tcl, the first element in a list has the index 0.

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

2 Comments

thanks, I'm used to lists being referenced by index implicitly.
@some1 The style you're talking about is more OO-like. Tcl's objects and classes tend to be heavier weight; they're not usually used for simple collection datatypes like lists or associative maps. (It would be possible to hack something in with unknown, but it would be a gross hack, slow and prone to not working as expected.)

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.