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.
6 '\" See the file "license.terms" for information on usage and redistribution
7 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 .TH scan n 8.4 Tcl "Tcl Built-In Commands"
10 .\" The -*- nroff -*- definitions below are for supplemental macros used
11 .\" in Tcl/Tk manual entries.
13 .\" .AP type name in/out ?indent?
14 .\" Start paragraph describing an argument to a library procedure.
15 .\" type is type of argument (int, etc.), in/out is either "in", "out",
16 .\" or "in/out" to describe whether procedure reads or modifies arg,
17 .\" and indent is equivalent to second arg of .IP (shouldn't ever be
18 .\" needed; use .AS below instead)
21 .\" Give maximum sizes of arguments for setting tab stops. Type and
22 .\" name are examples of largest possible arguments that will be passed
23 .\" to .AP later. If args are omitted, default tab stops are used.
26 .\" Start box enclosure. From here until next .BE, everything will be
27 .\" enclosed in one large box.
30 .\" End of box enclosure.
33 .\" Begin code excerpt.
38 .\" .VS ?version? ?br?
39 .\" Begin vertical sidebar, for use in marking newly-changed parts
40 .\" of man pages. The first argument is ignored and used for recording
41 .\" the version when the .VS was added, so that the sidebars can be
42 .\" found and removed when they reach a certain age. If another argument
43 .\" is present, then a line break is forced before starting the sidebar.
46 .\" End of vertical sidebar.
49 .\" Begin an indented unfilled display.
52 .\" End of indented unfilled display.
55 .\" Start of list of standard options for a Tk widget. The manpage
56 .\" argument defines where to look up the standard options; if
57 .\" omitted, defaults to "options". The options follow on successive
58 .\" lines, in three columns separated by tabs.
61 .\" End of list of standard options for a Tk widget.
63 .\" .OP cmdName dbName dbClass
64 .\" Start of description of a specific option. cmdName gives the
65 .\" option's name as specified in the class command, dbName gives
66 .\" the option's name in the option database, and dbClass gives
67 .\" the option's class in the option database.
70 .\" Print arg1 underlined, then print arg2 normally.
73 .\" Print arg1 in quotes, then arg2 normally (for trailing punctuation).
76 .\" Print an open parenthesis, arg1 in quotes, then arg2 normally
77 .\" (for trailing punctuation) and then a closing parenthesis.
79 .\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages.
83 .\" # Start an argument description
87 . ie !"\\$2"" .TP \\n()Cu
92 \&\\$1 \\fI\\$2\\fP (\\$3)
105 .\" # define tabbing values for .AP
108 .if !"\\$1"" .nr )A \\w'\\$1'u+3n
111 .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
112 .nr )C \\n()Bu+\\w'(in/out)'u+2n
114 .AS Tcl_Interp Tcl_CreateInterp in/out
115 .\" # BS - start boxed text
116 .\" # ^y = starting y location
124 .if n \l'\\n(.lu\(ul'
127 .\" # BE - end boxed text (draw box now)
132 .ie n \l'\\n(^lu\(ul'
134 .\" Draw four-sided box normally, but don't draw top of
135 .\" box if the box started on an earlier page.
137 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
140 \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
147 .\" # VS - start vertical sidebar
148 .\" # ^Y = starting y location
149 .\" # ^v = 1 (for troff; for nroff this doesn't matter)
153 .ie n 'mc \s12\(br\s0
156 .\" # VE - end of vertical sidebar
164 \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
171 .\" # Special macro to handle page bottom: finish off current
172 .\" # box/sidebar if in box/sidebar mode, then invoked standard
173 .\" # page bottom macro.
180 .\" Draw three-sided box if this is the box's first page,
181 .\" draw two sides but no top otherwise.
182 .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
183 .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
186 .nr ^x \\n(^tu+1v-\\n(^Yu
187 \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
200 .\" # DS - begin display
206 .\" # DE - end display
212 .\" # SO - start of list of standard options
214 'ie '\\$1'' .ds So \\fBoptions\\fR
215 'el .ds So \\fB\\$1\\fR
216 .SH "STANDARD OPTIONS"
222 .\" # SE - end of list of standard options
227 See the \\*(So manual entry for details on the standard options.
229 .\" # OP - start of full description for a single option
234 Command-Line Name: \\fB\\$1\\fR
235 Database Name: \\fB\\$2\\fR
236 Database Class: \\fB\\$3\\fR
240 .\" # CS - begin code excerpt
246 .\" # CE - end code excerpt
251 .\" # UL - underline word
255 .\" # QW - apply quotation marks to word
257 .ie '\\*(lq'"' ``\\$1''\\$2
258 .\"" fix emacs highlighting
259 .el \\*(lq\\$1\\*(rq\\$2
261 .\" # PQ - apply parens and quotation marks to word
263 .ie '\\*(lq'"' (``\\$1''\\$2)\\$3
264 .\"" fix emacs highlighting
265 .el (\\*(lq\\$1\\*(rq\\$2)\\$3
267 .\" # QR - quoted range
269 .ie '\\*(lq'"' ``\\$1''\\-``\\$2''\\$3
270 .\"" fix emacs highlighting
271 .el \\*(lq\\$1\\*(rq\\-\\*(lq\\$2\\*(rq\\$3
273 .\" # MT - "empty" string
278 '\" Note: do not modify the .SH NAME line immediately below!
280 scan \- Parse string using conversion specifiers in the style of sscanf
282 \fBscan \fIstring format \fR?\fIvarName varName ...\fR?
286 This command parses substrings from an input string in a fashion similar
287 to the ANSI C \fBsscanf\fR procedure and returns a count of the number of
288 conversions performed, or -1 if the end of the input string is reached
289 before any conversions have been performed. \fIString\fR gives the input
290 to be parsed and \fIformat\fR indicates how to parse it, using \fB%\fR
291 conversion specifiers as in \fBsscanf\fR. Each \fIvarName\fR gives the
292 name of a variable; when a substring is scanned from \fIstring\fR that
293 matches a conversion specifier, the substring is assigned to the
294 corresponding variable.
295 If no \fIvarName\fR variables are specified, then \fBscan\fR works in an
296 inline manner, returning the data that would otherwise be stored in the
297 variables as a list. In the inline case, an empty string is returned when
298 the end of the input string is reached before any conversions have been
300 .SH "DETAILS ON SCANNING"
302 \fBScan\fR operates by scanning \fIstring\fR and \fIformat\fR together.
303 If the next character in \fIformat\fR is a blank or tab then it
304 matches any number of white space characters in \fIstring\fR (including
306 Otherwise, if it is not a \fB%\fR character then it
307 must match the next character of \fIstring\fR.
308 When a \fB%\fR is encountered in \fIformat\fR, it indicates
309 the start of a conversion specifier.
310 A conversion specifier contains up to four fields after the \fB%\fR:
311 a XPG3 position specifier (or a \fB*\fR to indicate the converted
312 value is to be discarded instead of assigned to any variable); a number
313 indicating a maximum substring width; a size modifier; and a
314 conversion character.
315 All of these fields are optional except for the conversion character.
316 The fields that are present must appear in the order given above.
318 When \fBscan\fR finds a conversion specifier in \fIformat\fR, it
319 first skips any white-space characters in \fIstring\fR (unless the
320 conversion character is \fB[\fR or \fBc\fR).
321 Then it converts the next input characters according to the
322 conversion specifier and stores the result in the variable given
323 by the next argument to \fBscan\fR.
324 .SS "OPTIONAL POSITIONAL SPECIFIER"
326 If the \fB%\fR is followed by a decimal number and a \fB$\fR, as in
328 then the variable to use is not taken from the next
329 sequential argument. Instead, it is taken from the argument indicated
330 by the number, where 1 corresponds to the first \fIvarName\fR. If
331 there are any positional specifiers in \fIformat\fR then all of the
332 specifiers must be positional. Every \fIvarName\fR on the argument
333 list must correspond to exactly one conversion specifier or an error
334 is generated, or in the inline case, any position can be specified
335 at most once and the empty positions will be filled in with empty strings.
336 .SS "OPTIONAL SIZE MODIFIER"
338 The size modifier field is used only when scanning a substring into
339 one of Tcl's integer values. The size modifier field dictates the
340 integer range acceptable to be stored in a variable, or, for the inline
341 case, in a position in the result list.
342 The syntactically valid values for the size modifier are \fBh\fR, \fBL\fR,
343 \fBl\fR, and \fBll\fR. The \fBh\fR size modifier value is equivalent
344 to the absence of a size modifier in the the conversion specifier.
345 Either one indicates the integer range to be stored is limited to
346 the same range produced by the \fBint()\fR function of the \fBexpr\fR
347 command. The \fBL\fR size modifier is equivalent to the \fBl\fR size
348 modifier. Either one indicates the integer range to be stored is
349 limited to the same range produced by the \fBwide()\fR function of
350 the \fBexpr\fR command. The \fBll\fR size modifier indicates that
351 the integer range to be stored is unlimited.
352 .SS "MANDATORY CONVERSION CHARACTER"
354 The following conversion characters are supported:
358 The input substring must be a decimal integer.
359 It is read in and the integer value is stored in the variable,
360 truncated as required by the size modifier value.
364 The input substring must be an octal integer. It is read in and the
365 integer value is stored in the variable,
366 truncated as required by the size modifier value.
370 The input substring must be a hexadecimal integer.
371 It is read in and the integer value is stored in the variable,
372 truncated as required by the size modifier value.
376 The input substring must be a binary integer.
377 It is read in and the integer value is stored in the variable,
378 truncated as required by the size modifier value.
382 The input substring must be a decimal integer.
383 The integer value is truncated as required by the size modifier
384 value, and the corresponding unsigned value for that truncated
385 range is computed and stored in the variable as a decimal string.
386 The conversion makes no sense without reference to a truncation range,
387 so the size modifier \fBll\fR is not permitted in combination
388 with conversion character \fBu\fR.
392 The input substring must be an integer. The base (i.e. decimal, binary,
393 octal, or hexadecimal) is determined in the same fashion as described in
394 \fBexpr\fR. The integer value is stored in the variable,
395 truncated as required by the size modifier value.
399 A single character is read in and its Unicode value is stored in
400 the variable as an integer value.
401 Initial white space is not skipped in this case, so the input
402 substring may be a white-space character.
406 The input substring consists of all the characters up to the next
407 white-space character; the characters are copied to the variable.
409 \fBe\fR or \fBf\fR or \fBg\fR or \fBE\fR or \fBG\fR
411 The input substring must be a floating-point number consisting
412 of an optional sign, a string of decimal digits possibly
413 containing a decimal point, and an optional exponent consisting
414 of an \fBe\fR or \fBE\fR followed by an optional sign and a string of
416 It is read in and stored in the variable as a floating-point value.
420 The input substring consists of one or more characters in \fIchars\fR.
421 The matching string is stored in the variable.
422 If the first character between the brackets is a \fB]\fR then
423 it is treated as part of \fIchars\fR rather than the closing
426 contains a sequence of the form \fIa\fB\-\fIb\fR then any
427 character between \fIa\fR and \fIb\fR (inclusive) will match.
428 If the first or last character between the brackets is a \fB\-\fR, then
429 it is treated as part of \fIchars\fR rather than indicating a range.
433 The input substring consists of one or more characters not in \fIchars\fR.
434 The matching string is stored in the variable.
435 If the character immediately following the \fB^\fR is a \fB]\fR then it is
436 treated as part of the set rather than the closing bracket for
439 contains a sequence of the form \fIa\fB\-\fIb\fR then any
440 character between \fIa\fR and \fIb\fR (inclusive) will be excluded
442 If the first or last character between the brackets is a \fB\-\fR, then
443 it is treated as part of \fIchars\fR rather than indicating a range value.
447 No input is consumed from the input string. Instead, the total number
448 of characters scanned from the input string so far is stored in the variable.
450 The number of characters read from the input for a conversion is the
451 largest number that makes sense for that particular conversion (e.g.
452 as many decimal digits as possible for \fB%d\fR, as
453 many octal digits as possible for \fB%o\fR, and so on).
454 The input substring for a given conversion terminates either when a
455 white-space character is encountered or when the maximum substring
456 width has been reached, whichever comes first.
457 If a \fB*\fR is present in the conversion specifier
458 then no variable is assigned and the next scan argument is not consumed.
459 .SH "DIFFERENCES FROM ANSI SSCANF"
461 The behavior of the \fBscan\fR command is the same as the behavior of
462 the ANSI C \fBsscanf\fR procedure except for the following differences:
464 \fB%p\fR conversion specifier is not supported.
466 For \fB%c\fR conversions a single character value is
467 converted to a decimal string, which is then assigned to the
468 corresponding \fIvarName\fR;
469 no substring width may be specified for this conversion.
471 The \fBh\fR modifier is always ignored and the \fBl\fR and \fBL\fR
472 modifiers are ignored when converting real values (i.e. type
473 \fBdouble\fR is used for the internal representation). The \fBll\fR
474 modifier has no \fBsscanf\fR counterpart.
476 If the end of the input string is reached before any conversions have been
477 performed and no variables are given, an empty string is returned.
480 Convert a UNICODE character to its numeric value:
484 set value [\fBscan\fR $char %c]
487 Parse a simple color specification of the form \fI#RRGGBB\fR using
488 hexadecimal conversions with substring sizes:
492 \fBscan\fR $string "#%2x%2x%2x" r g b
495 Parse a \fIHH:MM\fR time string, noting that this avoids problems with
496 octal numbers by forcing interpretation as decimals (if we did not
497 care, we would use the \fB%i\fR conversion instead):
500 set string "08:08" ;# *Not* octal!
501 if {[\fBscan\fR $string "%d:%d" hours minutes] != 2} {
502 error "not a valid time string"
504 # We have to understand numeric ranges ourselves...
505 if {$minutes < 0 || $minutes > 59} {
506 error "invalid number of minutes"
510 Break a string up into sequences of non-whitespace characters (note
511 the use of the \fB%n\fR conversion so that we get skipping over
512 leading whitespace correct):
515 set string " a string {with braced words} + leading space "
517 while {[\fBscan\fR $string %s%n word length] == 2} {
519 set string [string range $string $length end]
523 Parse a simple coordinate string, checking that it is complete by
524 looking for the terminating character explicitly:
527 set string "(5.2,-4e-2)"
528 # Note that the spaces before the literal parts of
529 # the scan pattern are significant, and that ")" is
530 # the Unicode character \eu0029
532 [\fBscan\fR $string " (%f ,%f %c" x y last] != 3
535 error "invalid coordinate string"
540 An interactive session demonstrating the truncation of integer
541 values determined by size modifiers:
544 \fI%\fR set tcl_platform(wordSize)
546 \fI%\fR scan 20000000000000000000 %d
548 \fI%\fR scan 20000000000000000000 %ld
550 \fI%\fR scan 20000000000000000000 %lld
556 conversion specifier, parse, scan