Updated 2018-08-16 07:13:46 by AMG
dict map {keyVar valueVar} dictionaryValue body

This command is used to apply a transformation (described by body) to the dictionaryValue to produce a new dictionary. It is analogous to dict for as foreach is to lmap.

The new value created in the result dictionary will be the result of evaluating body that time round the loop, and the key used will be the current value of the variable keyVar (i.e., change the variable to change the key). It's an error to have the variable unset at that point; skip a round by using continue (or stop early with break).

[dict map] in Tcl 8.5 edit

AMG: The following implements [dict map] in Tcl 8.5 in terms of the other [dict] commands:
proc ::tcl::dict::map {keyVarValueVar dictionary script} {
    # Confirm argument syntax.
    if {[llength $keyVarValueVar] != 2} {
        return -code error "must have exactly two variable names"
    }

    # Link to local variables which will be used as iterators.
    upvar 1 [lindex $keyVarValueVar 0] key [lindex $keyVarValueVar 1] val

    # Accumulate and return the result.
    ::set result {}
    foreach {key val} [get $dictionary] {
        ::lappend result $key [uplevel 1 $script]
    }
    return $result
}
namespace ensemble configure ::dict -map [dict replace\
        [namespace ensemble configure ::dict -map] map ::tcl::dict::map]

For [dict map] in Tcl 8.4, see forward-compatible dict.