Version 15 of string map

Updated 2006-08-19 07:23:23

string map ?-nocase? charMap string

Replaces characters in string based on the key-value pairs in charMap. charMap is a list of key value key value ... as in the form returned by array get. Each instance of a key in the string will be replaced with its corresponding value. If -nocase is specified, then matching is done without regard to case differences. Both key and value may be multiple characters. Replacement is done in an ordered manner, so the key appearing first in the list will be checked first, and so on. string is only iterated over once, so earlier key replacements will have no effect for later key matches. For example,

   string map {abc 1 ab 2 a 3 1 0} 1abcaababcabababc

will return the string 01321221.


Read the information regarding string for the real details of this subcommand.

"string map ..." was introduced as one of the Changes in Tcl/Tk 8.1.1.

It provides the ability to translate portions of a string from one value to another.

For instance:

 % string map {abc 1 ab 2 a 3 1 0} 1abcaababcabababc
 01321221

As you can see, while you cannot use regular expressions to identify the strings to map, if you have a series of translations to perform, using string map means that you make one pass through the string rather than multiples.

[ Regular expressions are the weapon of choice for some people, to the extent that they fail to notice superior RE-free solutions. "string map ..." is worth learning, in the sense that it solves several important problems that are clumsy with REs.]

See Counting characters in a string on how string map is the best performer (again to RS's surprise...) - because it gets byte-compiled since 8.4.


Here's an example from jenglish that lvirden thought was pretty neat:

   string map -nocase {
     "&lt;"      "<"
     "&gt;"      ">"
     "&le;"      "<="
     "&ge;"      ">="
   } $whatever

See also:


IL 8/19/2006 How about a regex_map? In a language where we already have switch by regex, I think we can take this to the next level :). It's so trivial to do, but when included in the api itself, makes developers go ga-ga. then they get stuff like "out of the box" alot.

 proc regex_map { str args } {

    set alen [llength $args]
        if { $alen % 2 == 1} {
            set msg "wrong \# args: should be "
                append msg "regex value ?regex value?...\""
                return -code error $msg
        }

    foreach {regex value} $args {
        regsub -all $regex $str $value str
    }

    return $str
 }

 % regex_map "phil is a cool guy" {co+[^ ]} sweet {[pP]hil} Ivan
 Ivan is a sweet guy

Tcl syntax help - Category Command - Category String Processing