checking a value inside a list
checking a value inside a list
(OP)
I've got this variable named "ref":
802.11ac(auto),802.11n+a
Then I have this value, named "val":
802.11ac(auto)
so I want to check if $val is inside $ref
This is what I do:
set elements [split $ref ","]
if {([llength $elements] > 1) && ($val != "")} {
so it does B because I've checked and "[lsearch $elements $val]" is -1
However "puts [lindex $elements 0]" is 802.11ac(auto)
Anyone could tell me what I'm missing, please? Thanks a lot!
802.11ac(auto),802.11n+a
Then I have this value, named "val":
802.11ac(auto)
so I want to check if $val is inside $ref
This is what I do:
set elements [split $ref ","]
if {([llength $elements] > 1) && ($val != "")} {
if {[lsearch $elements $val] != -1} {
do A
}
else{
do B
}
}so it does B because I've checked and "[lsearch $elements $val]" is -1
However "puts [lindex $elements 0]" is 802.11ac(auto)
Anyone could tell me what I'm missing, please? Thanks a lot!
RE: checking a value inside a list
RE: checking a value inside a list
RE: checking a value inside a list
to check if val is inside of ref you can use the command
string first substring string
like this:
CODE
RE: checking a value inside a list
That was my first thought too, but without knowing the OP's intention, I decided to not suggest it yet.
To be noted that the initial code finds only comma separated exact matches, while the suggested code also finds partial matches :
CODE --> Tcl
CODE --> output
Feherke.
feherke.github.io
RE: checking a value inside a list
This is what I get so far:
CODE --> tcl
CODE --> outuput
I'm not sure, but maybe the parenthesis "()" are the characters that make the comparison not to work?
I'm not sure how to check what happens, maybe using string is? but what type should it be?
RE: checking a value inside a list
[lsearch -nocase $elements $valor]
to
[lsearch -nocase $elements $val]
I did it and it works for me.
Other problem could be that val contains trailing spaces, to see them print it with
puts "\"$val\""
if it contains spaces you can use string trim to remove them like:
CODE
@feherke: of course you are right. I haven't read natiya's description carefully enough.
RE: checking a value inside a list
I found the issue:
puts "val = \"$val\"" ---> val = "802.11ac(auto) "
so there was a space there!! MANY THANKS
The "$valor" was a typo when writing the post, sorry, I already corrected it.