Search code examples
tcl

Tcl how to remove only specified list item?


i need a solution to remove from a list only a specified file without removing other file. Now i'm using follow:

[lreplace $var end end]

where $var is a directory contain a list of file Example:

File1
File2
File3

Problem is if i launch "[lreplace $var end end]" it eliminate the last file but i want to eliminate ever and only "File 3" How i can modify it? Thanks


Solution

  • You want to lsearch for the wanted list item to get its index

    set mylist {File1 File3 File2}
    set idx [lsearch -exact $mylist "File3"]
    set newlist [lreplace $mylist $idx $idx]  ;# => {File1 File2}
    

    If the item is not in the list, then idx will be -1, and the lreplace invocation will not alter the original list.