简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM