Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking a value inside a list 1

Status
Not open for further replies.

Natiya

Programmer
May 14, 2017
12
0
0
ES
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 != "")} {
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!
 
What is the value of $valor? Aren't you meant to be checking $val?
 
Hello Natiya,

to check if val is inside of ref you can use the command
string first substring string
like this:

Code:
set ref "802.11ac(auto),802.11n+a"
set val "802.11ac(auto)"
if {[string first $val $ref] != -1} {
    puts "val found in ref"
} else {
    puts "val not found in ref"
}
 
Hi

mikrom said:
to check if val is inside of ref you can use the command
string first substring string
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:
[b]set[/b] ref [i][green]"802.11ac(auto),802.11n+a"[/green][/i]
[b]set[/b] val [i][green]"02.11ac"[/green][/i]

[b]puts[/b] [i][green]"Natiya [lsearch [split $ref "[/green][/i][teal],[/teal][i][green]"] $val]"[/green][/i]
[b]puts[/b] [i][green]"mikrom [string first $val $ref]"[/green][/i]
Code:
Natiya -1
mikrom 1


Feherke.
feherke.github.io
 
Thanks guys, but something weird is happening[3eyes]

This is what I get so far:


Code:
puts $ref 
puts $val
set elements [split $ref ","]
puts [lindex $elements 0]
puts [lsearch -nocase $elements $val]
puts [string first $val $ref]

Code:
[b]ref[/b] --> 802.11ac(auto),802.11n+a
[b]val[/b] --> 802.11ac(auto) 
[b]elements[/b] --> 802.11ac(auto) 802.11n+a
[b][lindex $elements 0][/b]-->802.11ac(auto)
[b][lsearch -nocase $elements $val][/b]-->-1
[b][string first $val $ref][/b]-->-1


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?
 
@Natiya: IMO you shoud correct
[lsearch -nocase $elements [highlight #FCE94F]$valor[/highlight]]
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:
puts "ref = \"$ref\"" 
puts "val = \"$val\""
set elements [split $ref ","]
puts "elements = \[$elements\]"
puts "elements\[0\] = \"[lindex $elements 0]\""
puts "lsearch: [lsearch -nocase $elements [string trim $val]]"
puts "string first: [string first [string trim $val] $ref]"

@feherke: of course you are right. I haven't read natiya's description carefully enough.
 
Thanks a lot @mikron!!

I found the issue:

puts "val = \"$val\"" ---> val = "802.11ac(auto) "

so there was a space there!! MANY THANKS [bigsmile]

The "$valor" was a typo when writing the post, sorry, I already corrected it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top