1

I have a "flat" Tcl list. Now I want to append a new element as a child to one of the existing elements. How can I do this?

This is what I tried:

[ lindex $flights $i ] [ lindex $flight 0 ] ]

I try to add an element form the list "flight" to an element of the list "flights". The element $i in the flights list already exists.

I might be running against Tcl syntax as I'm new to Tcl.

Thanks for your help.

1 Answer 1

2

You can use lset to replace an element of your list with a new list. http://www.tcl.tk/man/tcl8.5/TclCmd/lset.htm The first element of the new list will be the old element, the 2nd element will be its child. Here's an example:

% set flights [list a b c d e]
a b c d e
% set i 1
1
% lset flights $i [list b child]
a {b child} c d e
% lindex $flights 1
b child
% lindex [lindex $flights 1] 1
child
% lindex [lindex $flights 1] 0
b
Sign up to request clarification or add additional context in comments.

2 Comments

Also: lindex $flights 1 0 is useful, and in 8.6 you can use lset flights $i end+1 child. (Doesn't work in 8.5, which won't let lset extend lists.)
Thanks, I missed the list keyword.

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.