Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/scan.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

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