Search code examples
listtclbracketscurly-braces

lsearch does not match elements that require curly-braces (Tcl 8.4)


I'm dealing with a big number of signals. I've been able to store them into a list, but since their name have brackets the signals are store in a list. Latter on, using regexp, I analyze some output produced and, if there's a match, I needed to set a flag.

In this following example I show the element added to the list and, later one, I try to check if the same element is inside of the list using lsearch

set mylist [list]
set element {aux[1]}

lappend mylist $element
puts "mylist: $mylist \nelement: $element\n\[list element\]: [list $element]"

The result of this puts is:

mylist: {aux[1]}
element: aux[1]
[list element]: {aux[1]}

Since my element is stored as {a[1]}, I've not found a way to make lsearch to return a match

set result [lsearch $mylist $element]
set result2 [lsearch $mylist [list $element]]

puts $result
puts $result2

Both results return '-1'.

I've seen solutions, but none of them using Tcl 8.4; And I need to use it due to backwards compatibility.


Solution

  • Use the -exact matching style. The default style is -glob, which means that the substring [1] matches a single 1.

    lsearch -exact $mylist $element
    # => 0
    

    Documentation: lsearch