How te remove an element from TCL list say:
I have Googled and have not found any built-in function yet.
lset varName index newValue. The lset command can be used to set elements of a list directly, instead of using lreplace . Lists in Tcl are the right data structure to use when you have an arbitrary number of things, and you'd like to access them according to their order in the list. In C, you would use an array.
The other way to remove an element is to filter it out. This Tcl 8.5 technique differs from the lsearch & lreplace method mentioned elsewhere in that it removes all of a given element from the list. What it doesn't do is search through nested lists.
tcl Variables Deleting variable/sThe unset command is used to remove one or more variables.
Lappend is similar to append except that the values are appended as list elements rather than raw text. This command provides a relatively efficient way to build up large lists. For example, ``lappend a $b'' is much more efficient than ``set a [concat $a [list $b]]'' when $a is long.
set mylist {a b c} puts $mylist a b c
Remove by index
set mylist [lreplace $mylist 2 2] puts $mylist a b
Remove by value
set idx [lsearch $mylist "b"] set mylist [lreplace $mylist $idx $idx] puts $mylist a
The other way to remove an element is to filter it out. This Tcl 8.5 technique differs from the lsearch
&lreplace
method mentioned elsewhere in that it removes all of a given element from the list.
set stripped [lsearch -inline -all -not -exact $inputList $elemToRemove]
What it doesn't do is search through nested lists. That's a consequence of Tcl not putting effort into understanding your data structures too deeply. (You can tell it to search by comparing specific elements of the sublists though, via the -index
option.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With