Posted to tcl by jnc at Wed Nov 30 23:39:47 GMT 2011view raw
- set people {
- {John Doe Kansas}
- {Jane Jones Ohio}
- {Jane Smith Ohio}
- {Jane Doe Kansas}
- }
-
- # Searching using "AND" is easy when doing -inline
- set p $people
-
-
- puts "The starting list"
- puts $p
-
- set p [lsearch -index 0 -inline -all $p Jane]
- puts "Before the second search"
- puts $p\n
-
- set p [lsearch -index 2 -inline -all $p Ohio]
- puts "The final results"
- puts $p\n
-
- # Searching using "AND" is not so easy normally when returning the
- # indexes as you cannot then simply search the result. You need to
- # track multiple result lists searching the entire list with each
- # new condition and then pick out from the resulting multiple lists
- # only what exists in each list.
- #
- # However, using -in [list 2 3] you can limit the search and only
- # return matches with in the list indexes specified with the -in
- # option
-
- puts "Searching returning indexes:"
-
- set p $people
- set p [lsearch -index 0 -all $people Jane]
- puts "First search (Jane) narrowed the list indexes to: $p"
- set p [lsearch -index 2 -all -in $p $people Ohio]
- puts "Second search (Ohio) narrowed the list indexes to: $p"
-
- =====
-
- Output:
-
- The starting list
-
- {John Doe Kansas}
- {Jane Jones Ohio}
- {Jane Smith Ohio}
- {Jane Doe Kansas}
-
- Before the second search
- {Jane Jones Ohio} {Jane Smith Ohio} {Jane Doe Kansas}
-
- The final results
- {Jane Jones Ohio} {Jane Smith Ohio}
-
- Searching returning indexes:
- First search (Jane) narrowed the list indexes to: 1 2 3
- Second search (Ohio) narrowed the list indexes to: 1 2