user3889450
user3889450

Reputation: 11

How to replace TCL sublists

There is a list of list in Tcl.

set somelist {{aaa 1} {bbb 2} {ccc 1}}

How to replace the first list's element so the new list will look like I had done:

set somelist {{xxx 1} {bbb 2} {ccc 1}}

I tried to do this with lreplace, but it doesn't work.

lreplace somelist 0 1 {xxx 1}

Thanks.

Upvotes: 1

Views: 708

Answers (1)

glenn jackman
glenn jackman

Reputation: 246764

The lreplace command takes a list value as the first argument, not a variable name, so you would do

set somelist {{aaa 1} {bbb 2} {ccc 1}}
set somelist [lreplace $somelist 0 0 {xxx 1}]

Or, use lset, which is specifically for this case:

set somelist {{aaa 1} {bbb 2} {ccc 1}}
lset somelist 0 {xxx 1}

With lset, you can drill into sublists easily. For example, to change {bbb 2} to {bbb 42}, you could do

lset somelist {1 end} 42

Upvotes: 2

Related Questions