Reputation: 213
In the following list:
{12 34 23 54 67 75 46}
Using lsearch, what is the search pattern to be used for finding all the elements from this list which has the number '4' in it?
(i.e the output should return {34 54 46})
Upvotes: 0
Views: 1051
Reputation: 4813
The answer given by Brad Lanam will work if you know what you are matching will not contain any characters with a special meaning to glob. If that is not guaranteed, you can use:
set newlist [lsearch -all -inline -regexp $mylist (?q)4]
The (?q) makes anything following it a literal string. So no special interpretation of any characters will happen. But a regular expression is not anchored by default, so the string can appear anywhere in the list elements to match.
Upvotes: 0
Reputation: 5723
Reference: lsearch
You can use:
set newlist [lsearch -all -inline -glob $mylist *4*]
-glob
is the default, I put it in for documentation purposes.
-all
indicates to return all results, not just the first match.
-inline
indicates to return the list as the result.
Upvotes: 2