2

I have a list containing one member, that member is the string <cmd_stichstudy1>XXDDR0_MA[12]. When I search for that string in the list (using lsearch) I get that the list doesn't contain it. I even get it when I search for the member of the list:

tcl> set nets_names

{<cmd_stichstudy1>XXDDR0_MA[12]}

tcl> lsearch $nets_names [lindex $nets_names 0]

-1

Why does this happen?

SIMEL
  • 8,745
  • 28
  • 84
  • 130
  • 2
    Just to expand on the correct answers, because the search string contains square brackets, with glob-style matching, `lsearch` is trying to match either `XXDDR0_MA1` or `XXDDR0_MA2` -- documented on the [`string match` man page](http://tcl.tk/man/tcl8.5/TclCmd/string.htm#M40) – glenn jackman Sep 04 '12 at 16:00

2 Answers2

4

If you use -exact it will work the way you want.

% set nets_names {<cmd_stichstudy1>XXDDR0_MA[12]}
<cmd_stichstudy1>XXDDR0_MA[12]
% lsearch -exact $nets_names [lindex $nets_names 0]
0
%
TrojanName
  • 4,853
  • 5
  • 29
  • 41
2

lsearch has an unfortunate property of using glob-style matching by default.

To cite the manual:

If all matching style options are omitted, the default matching style is -glob.

So always pass -exact to lsearch unless you really want -glob.

kostix
  • 51,517
  • 14
  • 93
  • 176