dict probe

CMcC 2008Sep1 was looking for a way to represent a hierarchy of key/value pairs with wildcard matching.

The following proc searches for probe in the dict args, with the additional feature that each dict key may be a glob string or a list of keys. Thus [dict_probe {fred daughter} {f* wilma} {daughter pebbles} {barney} {son bambam}}] -> pebbles

   proc dict_probe {probe args} {
       if {[llength $args] == 1} {
           set args [lindex $args 0]
       }

       while {[llength $args] > 1 && [llength $probe] > 0} {
           # we still have a dict to search and a probe to find
           foreach {key val} $args {
               set found 0
               set p [lindex $probe 0]        ;# next probe element
               # probe each element of key 
               foreach m $key {
                   if {[string match $m $p]} {
                       # found a matching element
                       if {[llength $probe] == 1} {
                           # finished the probe
                           return [list $probe $val]
                       } else {
                           # repeat search at next level
                           set probe [lrange $probe 1 end]
                           set args $val        ;# probe this dict element
                           set found 1
                           break
                       }
                   }
                   if {$found} break
               }
               if {$found} break
           }
       }

       return [list $probe $args]
   }

DKF: I don't understand what it is doing, but here's how to plumb it into dict:

set map [namespace ensemble configure dict -map]
dict set map probe [list [namespace which dict_probe]]
namespace ensemble configure dict -map $map

See dict.