Version 13 of scan

Updated 2005-12-17 00:33:33

See http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/scan.htm for the formal man page for the Tcl command scan.

 scan $possiblyZeroedDecimal %d cleanInteger

Context: Leading zeroes induce octal parsing in the expr command and in Context: Leading zeroes induce octal parsing in the expr command and in Use of scan to remove leading zeros can avoid this often-reported problem.


display and numeric format for ASCII characters. display and numeric format for ASCII characters. Scan provides the usual answer:

    foreach character {a A b B c} {
        scan $character %c numeric
        puts "ASCII character '$numeric' displays as '$character'."
    }

    ASCII character '97' displays as 'a'.
    ASCII character '65' displays as 'A'.
    ASCII character '98' displays as 'b'.
    ASCII character '66' displays as 'B'.
    ASCII character '99' displays as 'c'.        

In more recent Tcl's (8.3 or so up) you also can call scan inline:

 set numeric [scan $character %c]

And the %c format is not limited to ASCII, but can handle any Unicode. (RS)

LV Can someone explain what this means - calling scan inline? Where is scan getting its input in this case? From stdin?

Unmatched Conversion Specifiers

Lars H: It refers to the result of scan, not its input. Rather than setting some variables, it will return a list of the parsed values. (RS's example is rather ugly, since it really sets $numeric to a list with one element, but as quick character code look-up at a prompt, a scan char %c is hard to beat.) Alastair Davies/PYK: When scan returns a list of matching values, it includes an empty string for each format specifier that wasn't matched. If there is only one format specifier, a list with one blank element is returned, and this is not the same thing as and empty string. For example, scan foo %d returns {}, which not an empty string, but a literal open-brace character followed by a literal close-brace character. The documentation states:


Also see "Dump a file in hex and ASCII", as well as "u2x" in the "Bag of algorithms".

Binary has a scan which complements use of this scan.

Effect on the Internal Representation of a Value


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