Skip to content

scan

Parse a string according to a format specification.

Syntax

tcl
scan string format ?varName ...?

Parameters

  • string: The string to parse
  • format: A format string with conversion specifiers (like C scanf)
  • varName: Variable names to store the parsed values

Description

Parses the input string according to the format specification and stores the results in the specified variables. Returns the number of conversions successfully performed. If no variable names are provided, returns a list of the parsed values. Returns -1 when the end of the input string is reached before any conversions have been performed.

Format Specifiers

SpecifierDescription
%dDecimal integer
%uUnsigned decimal integer
%oOctal integer
%x, %XHexadecimal integer
%bBinary integer
%iAuto-detect base integer (0x for hex, 0 for octal, otherwise decimal)
%f, %e, %E, %g, %GFloating-point number
%sNon-whitespace string
%cSingle character (returns Unicode codepoint value)
%[chars]Character set matching
%[^chars]Negated character set
%nCount of characters scanned so far
%%Literal percent sign

Format Features

FeatureExampleDescription
Field width%10sLimits to 10 characters
Suppression%*dDiscards the value (not stored)
Positional specifiers%2$dAssigns to 2nd variable
Size modifiers%ld, %lldParsed for compatibility
Character ranges%[a-z]Matches lowercase letters
Bracket in charset%[]abc]] as first character matches literally

Return Value Modes

ModeDescription
Variable modeWith varNames, returns count of successful conversions
Inline modeWithout varNames, returns list of values
EOF detectionReturns -1 when input exhausted before conversion

Unicode Character Handling

The %c specifier reads a single Unicode character and returns its codepoint value. This correctly handles multi-byte UTF-8 sequences:

  • ASCII characters: Returns values 0-127
  • 2-byte UTF-8: Returns codepoints U+0080 to U+07FF
  • 3-byte UTF-8: Returns codepoints U+0800 to U+FFFF
  • 4-byte UTF-8: Returns codepoints U+10000 to U+10FFFF (including emoji)

Examples

Basic parsing

Output

Parsing without variables

Output

Parsing floating-point

Output

Hexadecimal parsing

Output

Parsing fixed-width fields

Output

Count successful conversions

Output

Binary parsing

Output

Auto-detect base with %i

Output

Unicode character codepoint

Output

Suppression with %*

Output

See Also