-end-index in lsearch in tcl

lsearch command has -start index as one of the options

-start index Index search begins with the position index. If the index is end , it refers to the last element in the list and end - integer refers to the last element in the list minus the specified integer offset.

I would like to use -end along with -start . How can I do that? This can be done by dropping indexes greater than or equal to the -end index in the returned lsearch list. But is there a better way?

+4
source share
1 answer

In your case, you will be tempted to use lrange to create a copy of the list you are viewing without items that you do not want to return.

 lsearch [lrange $theList $start $end] $searchTerm 

The real purpose of the -start option is to allow skipping previously found matches, and is now less useful when we have the -all option (which makes lsearch return a list of all places where it can match the search query). It was used something like this:

 for {set idx -1} {[set idx [lsearch -start [incr idx] $list $term]] >= 0} {} { # Process the match index... puts "found $term at $idx" } 

And now you should write:

 foreach idx [lsearch -all $list $term] { puts "found $term at $idx" } 
+4
source

All Articles