Posted to tcl by jnc at Wed Nov 30 23:39:47 GMT 2011view raw

  1. set people {
  2. {John Doe Kansas}
  3. {Jane Jones Ohio}
  4. {Jane Smith Ohio}
  5. {Jane Doe Kansas}
  6. }
  7.  
  8. # Searching using "AND" is easy when doing -inline
  9. set p $people
  10.  
  11.  
  12. puts "The starting list"
  13. puts $p
  14.  
  15. set p [lsearch -index 0 -inline -all $p Jane]
  16. puts "Before the second search"
  17. puts $p\n
  18.  
  19. set p [lsearch -index 2 -inline -all $p Ohio]
  20. puts "The final results"
  21. puts $p\n
  22.  
  23. # Searching using "AND" is not so easy normally when returning the
  24. # indexes as you cannot then simply search the result. You need to
  25. # track multiple result lists searching the entire list with each
  26. # new condition and then pick out from the resulting multiple lists
  27. # only what exists in each list.
  28. #
  29. # However, using -in [list 2 3] you can limit the search and only
  30. # return matches with in the list indexes specified with the -in
  31. # option
  32.  
  33. puts "Searching returning indexes:"
  34.  
  35. set p $people
  36. set p [lsearch -index 0 -all $people Jane]
  37. puts "First search (Jane) narrowed the list indexes to: $p"
  38. set p [lsearch -index 2 -all -in $p $people Ohio]
  39. puts "Second search (Ohio) narrowed the list indexes to: $p"
  40.  
  41. =====
  42.  
  43. Output:
  44.  
  45. The starting list
  46.  
  47. {John Doe Kansas}
  48. {Jane Jones Ohio}
  49. {Jane Smith Ohio}
  50. {Jane Doe Kansas}
  51.  
  52. Before the second search
  53. {Jane Jones Ohio} {Jane Smith Ohio} {Jane Doe Kansas}
  54.  
  55. The final results
  56. {Jane Jones Ohio} {Jane Smith Ohio}
  57.  
  58. Searching returning indexes:
  59. First search (Jane) narrowed the list indexes to: 1 2 3
  60. Second search (Ohio) narrowed the list indexes to: 1 2