| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" Copyright (c) 2000 Scriptics Corporation. |
| 5 |
'\" |
| 6 |
'\" See the file "license.terms" for information on usage and redistribution |
| 7 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 8 |
'\" |
| 9 |
.TH scan n 8.4 Tcl "Tcl Built-In Commands" |
| 10 |
.so man.macros |
| 11 |
.BS |
| 12 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 13 |
.SH NAME |
| 14 |
scan \- Parse string using conversion specifiers in the style of sscanf |
| 15 |
.SH SYNOPSIS |
| 16 |
\fBscan \fIstring format \fR?\fIvarName varName ...\fR? |
| 17 |
.BE |
| 18 |
.SH INTRODUCTION |
| 19 |
.PP |
| 20 |
This command parses substrings from an input string in a fashion similar |
| 21 |
to the ANSI C \fBsscanf\fR procedure and returns a count of the number of |
| 22 |
conversions performed, or -1 if the end of the input string is reached |
| 23 |
before any conversions have been performed. \fIString\fR gives the input |
| 24 |
to be parsed and \fIformat\fR indicates how to parse it, using \fB%\fR |
| 25 |
conversion specifiers as in \fBsscanf\fR. Each \fIvarName\fR gives the |
| 26 |
name of a variable; when a substring is scanned from \fIstring\fR that |
| 27 |
matches a conversion specifier, the substring is assigned to the |
| 28 |
corresponding variable. |
| 29 |
If no \fIvarName\fR variables are specified, then \fBscan\fR works in an |
| 30 |
inline manner, returning the data that would otherwise be stored in the |
| 31 |
variables as a list. In the inline case, an empty string is returned when |
| 32 |
the end of the input string is reached before any conversions have been |
| 33 |
performed. |
| 34 |
.SH "DETAILS ON SCANNING" |
| 35 |
.PP |
| 36 |
\fBScan\fR operates by scanning \fIstring\fR and \fIformat\fR together. |
| 37 |
If the next character in \fIformat\fR is a blank or tab then it |
| 38 |
matches any number of white space characters in \fIstring\fR (including |
| 39 |
zero). |
| 40 |
Otherwise, if it is not a \fB%\fR character then it |
| 41 |
must match the next character of \fIstring\fR. |
| 42 |
When a \fB%\fR is encountered in \fIformat\fR, it indicates |
| 43 |
the start of a conversion specifier. |
| 44 |
A conversion specifier contains up to four fields after the \fB%\fR: |
| 45 |
a XPG3 position specifier (or a \fB*\fR to indicate the converted |
| 46 |
value is to be discarded instead of assigned to any variable); a number |
| 47 |
indicating a maximum substring width; a size modifier; and a |
| 48 |
conversion character. |
| 49 |
All of these fields are optional except for the conversion character. |
| 50 |
The fields that are present must appear in the order given above. |
| 51 |
.PP |
| 52 |
When \fBscan\fR finds a conversion specifier in \fIformat\fR, it |
| 53 |
first skips any white-space characters in \fIstring\fR (unless the |
| 54 |
conversion character is \fB[\fR or \fBc\fR). |
| 55 |
Then it converts the next input characters according to the |
| 56 |
conversion specifier and stores the result in the variable given |
| 57 |
by the next argument to \fBscan\fR. |
| 58 |
.SS "OPTIONAL POSITIONAL SPECIFIER" |
| 59 |
.PP |
| 60 |
If the \fB%\fR is followed by a decimal number and a \fB$\fR, as in |
| 61 |
.QW \fB%2$d\fR , |
| 62 |
then the variable to use is not taken from the next |
| 63 |
sequential argument. Instead, it is taken from the argument indicated |
| 64 |
by the number, where 1 corresponds to the first \fIvarName\fR. If |
| 65 |
there are any positional specifiers in \fIformat\fR then all of the |
| 66 |
specifiers must be positional. Every \fIvarName\fR on the argument |
| 67 |
list must correspond to exactly one conversion specifier or an error |
| 68 |
is generated, or in the inline case, any position can be specified |
| 69 |
at most once and the empty positions will be filled in with empty strings. |
| 70 |
.SS "OPTIONAL SIZE MODIFIER" |
| 71 |
.PP |
| 72 |
The size modifier field is used only when scanning a substring into |
| 73 |
one of Tcl's integer values. The size modifier field dictates the |
| 74 |
integer range acceptable to be stored in a variable, or, for the inline |
| 75 |
case, in a position in the result list. |
| 76 |
The syntactically valid values for the size modifier are \fBh\fR, \fBL\fR, |
| 77 |
\fBl\fR, and \fBll\fR. The \fBh\fR size modifier value is equivalent |
| 78 |
to the absence of a size modifier in the the conversion specifier. |
| 79 |
Either one indicates the integer range to be stored is limited to |
| 80 |
the same range produced by the \fBint()\fR function of the \fBexpr\fR |
| 81 |
command. The \fBL\fR size modifier is equivalent to the \fBl\fR size |
| 82 |
modifier. Either one indicates the integer range to be stored is |
| 83 |
limited to the same range produced by the \fBwide()\fR function of |
| 84 |
the \fBexpr\fR command. The \fBll\fR size modifier indicates that |
| 85 |
the integer range to be stored is unlimited. |
| 86 |
.SS "MANDATORY CONVERSION CHARACTER" |
| 87 |
.PP |
| 88 |
The following conversion characters are supported: |
| 89 |
.TP |
| 90 |
\fBd\fR |
| 91 |
. |
| 92 |
The input substring must be a decimal integer. |
| 93 |
It is read in and the integer value is stored in the variable, |
| 94 |
truncated as required by the size modifier value. |
| 95 |
.TP |
| 96 |
\fBo\fR |
| 97 |
. |
| 98 |
The input substring must be an octal integer. It is read in and the |
| 99 |
integer value is stored in the variable, |
| 100 |
truncated as required by the size modifier value. |
| 101 |
.TP |
| 102 |
\fBx\fR or \fBX\fR |
| 103 |
. |
| 104 |
The input substring must be a hexadecimal integer. |
| 105 |
It is read in and the integer value is stored in the variable, |
| 106 |
truncated as required by the size modifier value. |
| 107 |
.TP |
| 108 |
\fBb\fR |
| 109 |
. |
| 110 |
The input substring must be a binary integer. |
| 111 |
It is read in and the integer value is stored in the variable, |
| 112 |
truncated as required by the size modifier value. |
| 113 |
.TP |
| 114 |
\fBu\fR |
| 115 |
. |
| 116 |
The input substring must be a decimal integer. |
| 117 |
The integer value is truncated as required by the size modifier |
| 118 |
value, and the corresponding unsigned value for that truncated |
| 119 |
range is computed and stored in the variable as a decimal string. |
| 120 |
The conversion makes no sense without reference to a truncation range, |
| 121 |
so the size modifier \fBll\fR is not permitted in combination |
| 122 |
with conversion character \fBu\fR. |
| 123 |
.TP |
| 124 |
\fBi\fR |
| 125 |
. |
| 126 |
The input substring must be an integer. The base (i.e. decimal, octal, or hexadecimal) is determined by the C convention (leading 0 for octal; prefix 0x for hexadecimal). The integer value is stored in the variable, |
| 127 |
truncated as required by the size modifier value. |
| 128 |
.TP |
| 129 |
\fBc\fR |
| 130 |
. |
| 131 |
A single character is read in and its Unicode value is stored in |
| 132 |
the variable as an integer value. |
| 133 |
Initial white space is not skipped in this case, so the input |
| 134 |
substring may be a white-space character. |
| 135 |
.TP |
| 136 |
\fBs\fR |
| 137 |
. |
| 138 |
The input substring consists of all the characters up to the next |
| 139 |
white-space character; the characters are copied to the variable. |
| 140 |
.TP |
| 141 |
\fBe\fR or \fBf\fR or \fBg\fR or \fBE\fR or \fBG\fR |
| 142 |
. |
| 143 |
The input substring must be a floating-point number consisting |
| 144 |
of an optional sign, a string of decimal digits possibly |
| 145 |
containing a decimal point, and an optional exponent consisting |
| 146 |
of an \fBe\fR or \fBE\fR followed by an optional sign and a string of |
| 147 |
decimal digits. |
| 148 |
It is read in and stored in the variable as a floating-point value. |
| 149 |
.TP |
| 150 |
\fB[\fIchars\fB]\fR |
| 151 |
. |
| 152 |
The input substring consists of one or more characters in \fIchars\fR. |
| 153 |
The matching string is stored in the variable. |
| 154 |
If the first character between the brackets is a \fB]\fR then |
| 155 |
it is treated as part of \fIchars\fR rather than the closing |
| 156 |
bracket for the set. |
| 157 |
If \fIchars\fR |
| 158 |
contains a sequence of the form \fIa\fB\-\fIb\fR then any |
| 159 |
character between \fIa\fR and \fIb\fR (inclusive) will match. |
| 160 |
If the first or last character between the brackets is a \fB\-\fR, then |
| 161 |
it is treated as part of \fIchars\fR rather than indicating a range. |
| 162 |
.TP |
| 163 |
\fB[^\fIchars\fB]\fR |
| 164 |
. |
| 165 |
The input substring consists of one or more characters not in \fIchars\fR. |
| 166 |
The matching string is stored in the variable. |
| 167 |
If the character immediately following the \fB^\fR is a \fB]\fR then it is |
| 168 |
treated as part of the set rather than the closing bracket for |
| 169 |
the set. |
| 170 |
If \fIchars\fR |
| 171 |
contains a sequence of the form \fIa\fB\-\fIb\fR then any |
| 172 |
character between \fIa\fR and \fIb\fR (inclusive) will be excluded |
| 173 |
from the set. |
| 174 |
If the first or last character between the brackets is a \fB\-\fR, then |
| 175 |
it is treated as part of \fIchars\fR rather than indicating a range value. |
| 176 |
.TP |
| 177 |
\fBn\fR |
| 178 |
. |
| 179 |
No input is consumed from the input string. Instead, the total number |
| 180 |
of characters scanned from the input string so far is stored in the variable. |
| 181 |
.PP |
| 182 |
The number of characters read from the input for a conversion is the |
| 183 |
largest number that makes sense for that particular conversion (e.g. |
| 184 |
as many decimal digits as possible for \fB%d\fR, as |
| 185 |
many octal digits as possible for \fB%o\fR, and so on). |
| 186 |
The input substring for a given conversion terminates either when a |
| 187 |
white-space character is encountered or when the maximum substring |
| 188 |
width has been reached, whichever comes first. |
| 189 |
If a \fB*\fR is present in the conversion specifier |
| 190 |
then no variable is assigned and the next scan argument is not consumed. |
| 191 |
.SH "DIFFERENCES FROM ANSI SSCANF" |
| 192 |
.PP |
| 193 |
The behavior of the \fBscan\fR command is the same as the behavior of |
| 194 |
the ANSI C \fBsscanf\fR procedure except for the following differences: |
| 195 |
.IP [1] |
| 196 |
\fB%p\fR conversion specifier is not supported. |
| 197 |
.IP [2] |
| 198 |
For \fB%c\fR conversions a single character value is |
| 199 |
converted to a decimal string, which is then assigned to the |
| 200 |
corresponding \fIvarName\fR; |
| 201 |
no substring width may be specified for this conversion. |
| 202 |
.IP [3] |
| 203 |
The \fBh\fR modifier is always ignored and the \fBl\fR and \fBL\fR |
| 204 |
modifiers are ignored when converting real values (i.e. type |
| 205 |
\fBdouble\fR is used for the internal representation). The \fBll\fR |
| 206 |
modifier has no \fBsscanf\fR counterpart. |
| 207 |
.IP [4] |
| 208 |
If the end of the input string is reached before any conversions have been |
| 209 |
performed and no variables are given, an empty string is returned. |
| 210 |
.SH EXAMPLES |
| 211 |
.PP |
| 212 |
Convert a UNICODE character to its numeric value: |
| 213 |
.PP |
| 214 |
.CS |
| 215 |
set char "x" |
| 216 |
set value [\fBscan\fR $char %c] |
| 217 |
.CE |
| 218 |
.PP |
| 219 |
Parse a simple color specification of the form \fI#RRGGBB\fR using |
| 220 |
hexadecimal conversions with substring sizes: |
| 221 |
.PP |
| 222 |
.CS |
| 223 |
set string "#08D03F" |
| 224 |
\fBscan\fR $string "#%2x%2x%2x" r g b |
| 225 |
.CE |
| 226 |
.PP |
| 227 |
Parse a \fIHH:MM\fR time string, noting that this avoids problems with |
| 228 |
octal numbers by forcing interpretation as decimals (if we did not |
| 229 |
care, we would use the \fB%i\fR conversion instead): |
| 230 |
.PP |
| 231 |
.CS |
| 232 |
set string "08:08" ;# *Not* octal! |
| 233 |
if {[\fBscan\fR $string "%d:%d" hours minutes] != 2} { |
| 234 |
error "not a valid time string" |
| 235 |
} |
| 236 |
# We have to understand numeric ranges ourselves... |
| 237 |
if {$minutes < 0 || $minutes > 59} { |
| 238 |
error "invalid number of minutes" |
| 239 |
} |
| 240 |
.CE |
| 241 |
.PP |
| 242 |
Break a string up into sequences of non-whitespace characters (note |
| 243 |
the use of the \fB%n\fR conversion so that we get skipping over |
| 244 |
leading whitespace correct): |
| 245 |
.PP |
| 246 |
.CS |
| 247 |
set string " a string {with braced words} + leading space " |
| 248 |
set words {} |
| 249 |
while {[\fBscan\fR $string %s%n word length] == 2} { |
| 250 |
lappend words $word |
| 251 |
set string [string range $string $length end] |
| 252 |
} |
| 253 |
.CE |
| 254 |
.PP |
| 255 |
Parse a simple coordinate string, checking that it is complete by |
| 256 |
looking for the terminating character explicitly: |
| 257 |
.PP |
| 258 |
.CS |
| 259 |
set string "(5.2,-4e-2)" |
| 260 |
# Note that the spaces before the literal parts of |
| 261 |
# the scan pattern are significant, and that ")" is |
| 262 |
# the Unicode character \eu0029 |
| 263 |
if { |
| 264 |
[\fBscan\fR $string " (%f ,%f %c" x y last] != 3 |
| 265 |
|| $last != 0x0029 |
| 266 |
} then { |
| 267 |
error "invalid coordinate string" |
| 268 |
} |
| 269 |
puts "X=$x, Y=$y" |
| 270 |
.CE |
| 271 |
.PP |
| 272 |
An interactive session demonstrating the truncation of integer |
| 273 |
values determined by size modifiers: |
| 274 |
.PP |
| 275 |
.CS |
| 276 |
\fI%\fR set tcl_platform(wordSize) |
| 277 |
4 |
| 278 |
\fI%\fR scan 20000000000000000000 %d |
| 279 |
2147483647 |
| 280 |
\fI%\fR scan 20000000000000000000 %ld |
| 281 |
9223372036854775807 |
| 282 |
\fI%\fR scan 20000000000000000000 %lld |
| 283 |
20000000000000000000 |
| 284 |
.CE |
| 285 |
.SH "SEE ALSO" |
| 286 |
format(n), sscanf(3) |
| 287 |
.SH KEYWORDS |
| 288 |
conversion specifier, parse, scan |
| 289 |
'\" Local Variables: |
| 290 |
'\" mode: nroff |
| 291 |
'\" End: |