Skip to main content

Posts

Showing posts from May, 2024

Remove named items from a list

Sometimes you just want to trim down a list of items but you're not quite certain where the items are so its not possible to use lreplace or lremove directly as these require indices. In order to obtain these lsearch can be used.  The following code example shows how wrap these operations into a single procedure call where mylist is the list to alter, and args a list of items to remove. The procedure returns a new list rather than modifying the original directly.   proc remove {mylist args} {          foreach value $args {         set idx [lsearch -exact $mylist $value]         set mylist [lreplace $mylist $idx $idx]     }          return $mylist }