Tcl scan Command
last modified April 3, 2025
The Tcl scan
command parses string data according to a format
specifier. It's similar to C's sscanf
function. The command
extracts values from strings and assigns them to variables.
Basic Definition
The scan
command interprets string data based on a format string.
It returns the number of successful conversions performed.
Syntax: scan string format ?varName varName ...?
. The format
specifier uses conversion characters similar to printf
.
Basic String Parsing
This example shows how to extract simple values from a string using scan
.
set data "John 25" scan $data "%s %d" name age puts "Name: $name, Age: $age"
Here we parse a string containing a name and age. The %s
format
extracts a string, while %d
extracts a decimal integer.
Hexadecimal Conversion
scan
can convert hexadecimal numbers to decimal values.
set hex "FF" scan $hex "%x" decimal puts "Hex $hex is $decimal in decimal"
This converts the hexadecimal value "FF" to its decimal equivalent 255.
The %x
format specifier handles hexadecimal conversion.
Floating Point Numbers
The command can extract floating point numbers from strings.
set measurement "12.5cm" scan $measurement "%f" value puts "Measurement: $value"
This extracts the floating point number 12.5 from the string "12.5cm".
The %f
format specifier handles floating point conversion.
Multiple Value Extraction
scan
can extract multiple values from a single string.
set coordinates "(3.14, 2.71)" scan $coordinates "(%f, %f)" x y puts "X: $x, Y: $y"
This parses two floating point numbers from a coordinate string. The format string matches the parentheses and comma in the input.
Character Extraction
Individual characters can be extracted using the %c
format.
set word "hello" scan $word "%c" first_char puts "First character: [format %c $first_char]"
This extracts the ASCII value of the first character 'h'. The format
command converts it back to a character for display.
Ignoring Characters
Characters can be skipped in the input string using %*
.
set data "Name: John, Age: 25" scan $data "Name: %s, Age: %d" name age puts "Name: $name, Age: $age"
This example skips the literal strings "Name: " and ", Age: " while extracting the variable values. The format string matches the input exactly.
Best Practices
- Format Matching: Ensure format string matches input structure.
- Error Handling: Check return value for successful conversions.
- Type Safety: Use correct format specifiers for data types.
- Whitespace: Be aware of whitespace handling in format strings.
- Performance: For complex parsing, consider regular expressions.
This tutorial covered the Tcl scan
command with practical
examples showing its usage for string parsing and conversion.
Author
List all Tcl Tutorials.