![]() |
![]() ![]() ![]() ![]() |
Parse string using conversion specifiers in the style of sscanf
scan string format varName ?varName ...?
This command parses fields from an input string in the same fashion as the ANSI C sscanf procedure and returns a count of the number of fields sucessfully parsed. String gives the input to be parsed and format indicates how to parse it, using % conversion specifiers as in sscanf. Each varName gives the name of a variable; when a field is scanned from string the result is converted back into a string and assigned to the corresponding variable.
Scan operates by scanning string and formatString together. If the next character in formatString is a blank or tab then it is ignored. Otherwise, if it isn't a % character then it must match the next non-white-space character of string. When a % is encountered in formatString, it indicates the start of a conversion specifier. A conversion specifier contains three fields after the %: a *, which indicates that the converted value is to be discarded instead of assigned to a variable; a number indicating a maximum field width; and a conversion character. All of these fields are optional except for the conversion character.
When scan finds a conversion specifier in formatString, it first skips any white-space characters in string. Then it converts the next input characters according to the conversion specifier and stores the result in the variable given by the next argument to scan. The following conversion characters are supported:
The number of characters read from the input for a conversion is the largest number that makes sense for that particular conversion (e.g. as many decimal digits as possible for %d, as many octal digits as possible for %o, and so on). The input field for a given conversion terminates either when a white-space character is encountered or when the maximum field width has been reached, whichever comes first. If a * is present in the conversion specifier then no variable is assigned and the next scan argument is not consumed.
The behavior of the scan command is the same as the behavior of the ANSI C sscanf procedure except for the following differences:
[1] %p and %n conversion specifiers are not currently supported.
[2] For %c conversions a single character value is converted to a decimal string, which is then assigned to the corresponding varName; no field width may be specified for this conversion.
[3] The l, h, and L modifiers are ignored; integer values are always converted as if there were no modifier present and real values are always converted as if the l modifier were present (i.e. type double is used for the internal representation).
conversion specifier, parse, scan