Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 4.  String Processing in Tcl

The scan Command

The scan command parses a string according to a format specification and assigns values to variables. It returns the number of successful conversions it made. The general form of the command is:

scan string format var ?var? ?var? ...

The format for scan is nearly the same as in the format command. There is no %u scan format. The %c scan format converts one character to its decimal value.

The scan format includes a set notation. Use square brackets to delimit a set of characters. The set matches one or more characters that are copied into the variable. A dash is used to specify a range. The following scans a field of all lowercase letters.

scan abcABC {%[a-z]}result
=> 1
set result
=> abc

If the first character in the set is a right square bracket, then it is considered part of the set. If the first character in the set is ^, then characters not in the set match. Again, put a right square bracket immediately after the ^ to include it in the set. Nothing special is required to include a left square bracket in the set. As in the previous example, you will want to protect the format with braces, or use backslashes, because square brackets are special to the Tcl parser.

      Previous section   Next section