×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

checking a value inside a list

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 != "")} {
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

What is the value of $valor? Aren't you meant to be checking $val?

RE: checking a value inside a list

(OP)
@xwb just edited that, thanks!

RE: checking a value inside a list

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"
} 

RE: checking a value inside a list

Hi

Quote (mikrom)

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 --> Tcl

set ref "802.11ac(auto),802.11n+a"
set val "02.11ac"

puts "Natiya [lsearch [split $ref ","] $val]"
puts "mikrom [string first $val $ref]" 

CODE --> output

Natiya -1
mikrom 1 

Feherke.
feherke.github.io

RE: checking a value inside a list

(OP)
Thanks guys, but something weird is happening3eyes

This is what I get so far:


CODE --> tcl

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

CODE --> outuput

ref --> 802.11ac(auto),802.11n+a
val --> 802.11ac(auto) 
elements --> 802.11ac(auto) 802.11n+a
[lindex $elements 0]-->802.11ac(auto)
[lsearch -nocase $elements $val]-->-1
[string first $val $ref]-->-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?

RE: checking a value inside a list

@Natiya: IMO you shoud correct
[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

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.

RE: checking a value inside a list

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close