How to check if list element exists in TCL?
Asked Answered
D

3

10

Say I have a TCL list, and I have append some elements to my list. Now I want to check if I have appended 6 or 7 elements.

In order to check if list element exists in the place specified by an index I have used:

if { [info exists [lindex $myList 6]] } {
#if I am here then I have appended 7 elems, otherwise it should be at least 6
}

But seams this does not work. How I should do that? properly? It is OK to check if { [lindex $myList 6]] eq "" }

Dinette answered 11/4, 2011 at 6:37 Comment(0)
V
21

I found this question because I wanted to check if a list contains a specific item, rather than just checking the list's length.

To see if an element exists within a list, use the lsearch function:

if {[lsearch -exact $myList 4] >= 0} {
    puts "Found 4 in myList!"
}

The lsearch function returns the index of the first found element or -1 if the given element wasn't found. Through the -exact, -glob (which is the default) or -regexp options, the type of pattern search can be specified.

Versicolor answered 10/7, 2015 at 8:4 Comment(0)
P
18

Why don't you use llength to check the length of your list:

if {[llength $myList] == 6} {
    # do something
}

Of course, if you want to check the element at a specific index, then then use lindex to retrieve that element and check that. e.g. if {[lindex $myList 6] == "something"}

Your code using the info exists is not working, because the info exists command checks if a variable exists. So you are basically checking if there is a variable whose name equals the value returned by [lindex $myList 6].

Plath answered 11/4, 2011 at 7:15 Comment(3)
What you mean my "something"? I have used if { [lindex $myList 6]] eq "" } in order to check if the 7th elem exist. If it does not exist, I am in the body of the if statement. Is it recommended to use?Dinette
"something" is just a random string I chose for my example. You can of course replace that with any string you like. The if {[lindex $myList 6]] == ""} will most likely work, but it does have a flaw. Suppose you have the following list: set l {1 2 3 4 5 6 ""}. This list has 7 elements (check the output of llength $l). But your code would treat this list as if it only had 6 elements. If you want to check if an element exists at index X, use llength to check if the list is that long, it's definitely more explicit. If you wan't to check if the element has a specific value use lindex.Baugh
Its worth noting that the solution the OP is using ([lindex $mylist 6]) isn't semantically equivalent to the question he's trying to ask (does this list have 6 elements). I'm a big proponent of code expressing what it's trying to accomplish. The answer posted here by überjesus ([llength $mylist] == 6) expresses the question that's actually being asked so, even if both worked correctly, this would be the better option.Midway
B
9

Another way to check for existence in a list in TCL is to simply use 'in', for instance:

if {"4" in $myList} {
    puts "Found 4 in my list"
}

It's slightly cleaner/more readable!

Backwash answered 8/8, 2019 at 17:52 Comment(1)
When did TCL become Python !Trawler

© 2022 - 2024 — McMap. All rights reserved.