tcl check if value exists in list code example

Example: tcl check if value exists in list

# Make List object
set List_to_Search [list 1 2 3 4 5 6 7 8 9];
# Identify item to search for:
set findMe 7;
# Search List using lsearch, returns index of first occurrence found
set index [lsearch $List_to_Search $findMe];
# > 6
puts "$findMe = [lindex $List_to_Search $index]";
# If object isn't contained in list - returns '-1' as index:
set notFound [lsearch $List_to_Search 99];
puts "99 not found in List;  $notFound";

Tags:

Misc Example