5

I want to dynamically add elements to nested lists. Consider the following example:

set super_list {}
lappend super_list {00 01 02}
lappend super_list {10 11 12}
lappend super_list {20 21}

results in:

super_list = {00 01 02} {10 11 12} {20 21}
[lindex $super_list 0] = {00 01 02}
[lindex $super_list 1] = {10 11 12}
[lindex $super_list 2] = {20 21}

How do I append another value (e.g. 22) to [lindex $super_list 2]?

lappend [lindex $super_list 2] 22

does not work!

The only workaround I could think of so far is:

lset super_list 2 [concat [lindex $super_list 2] {22}]

Is this really the only way?

Thanks, Linus

2 Answers 2

4

In Tcl 8.6 (the feature was added; it doesn't work in earlier versions) you can use lset to extend nested lists via the index end+1:

set super_list {{00 01 02} {10 11 12} {20 21}}
lset super_list 2 end+1 22
puts [lindex $super_list 2]
# ==>  20 21 22

You could address one past the end by using a numeric index too, but I think end+1 is more mnemonic.

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

Comments

2

There's no direct method for lists to do this. You could at least wrap it up in a proc:

proc sub_lappend {listname idx args} {
    upvar 1 $listname l
    set subl [lindex $l $idx]
    lappend subl {*}$args
    lset l $idx $subl
}
sub_lappend super_list 2 22 23 24
{00 01 02} {10 11 12} {20 21 22 23 24}

An advatange of this approach is you can pass a list of indices to work in arbitrarily nested lists (like lset):

% sub_lappend super_list {0 0} 00a 00b 00c
{{00 00a 00b 00c} 01 02} {10 11 12} {20 21 22 23 24}

1 Comment

There's a direct method in 8.6; in 8.5 and 8.4 the method you describe should be used. Prior to 8.4… it gets very complicated…

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.