TCL remove an element from a list

Narek picture Narek · Apr 18, 2011 · Viewed 76.3k times · Source

How te remove an element from TCL list say:

  1. which has index = 4
  2. which has value = "aa"

I have Googled and have not found any built-in function yet.

Answer

drysdam picture drysdam · Apr 18, 2011
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