[ Team LiB ] Previous Section Next Section

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, unless no capture variables are given, in which case it returns the scan matches in a list. 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. 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.

    [ Team LiB ] Previous Section Next Section