Version 6 of subst

Updated 2002-07-25 08:16:17

http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/subst.htm


What's the use of subst? As Joe English, who has deep experience in the area, explains, "[subst] is massively handy in text-processing applications, especially SGML and XML down-translators. [subst] and [ string map] make Tcl particularly well-suited for this type of task." Many jobs for which Perl uses "right-hand-side" variables with regular expression substitution, Tcl does as REs whose results are subst-ituted.


Very simple example of using [subst] with XML/HTML.

       set html   {<html><head>$title</head></html>}
       set title  "Hello, World!"
       set output [subst -nocommands $html]
       set output
       <html><head>Hello, World!</head></html>

Another alternative would be to use XPath I use something like this in an application of mine. Of course the "text" of the 'html' variable above would probably come from a file.

I would much rather use XPath to hunt down the place to change.


RS most often uses subst for expanding Unicodes: cross-platform, in mostly 8-bit environments, it is most robust to output Unicodes in the \u.... notation - such snippets can be pasted into a text widget and visualized by

 subst [$t get 1.0 end]

[Explain the regsub idiom.]


Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command