scan.n

来自「tcl是工具命令语言」· N 代码 · 共 218 行

N
218
字号
'\"'\" Copyright (c) 1993 The Regents of the University of California.'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.'\" Copyright (c) 2000 Scriptics Corporation.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" RCS: @(#) $Id: scan.n,v 1.9 2002/07/01 18:24:39 jenglish Exp $'\" .so man.macros.TH scan n 8.4 Tcl "Tcl Built-In Commands".BS'\" Note:  do not modify the .SH NAME line immediately below!.SH NAMEscan \- Parse string using conversion specifiers in the style of sscanf.SH SYNOPSIS\fBscan \fIstring format \fR?\fIvarName varName ...\fR?.BE.SH INTRODUCTION.PPThis command parses fields from an input string in the same fashion as theANSI C \fBsscanf\fR procedure and returns a count of the number ofconversions performed, or -1 if the end of the input string is reachedbefore any conversions have been performed.  \fIString\fR gives the inputto be parsed and \fIformat\fR indicates how to parse it, using \fB%\fRconversion specifiers as in \fBsscanf\fR.  Each \fIvarName\fR gives thename of a variable; when a field is scanned from \fIstring\fR the result isconverted back into a string and assigned to the corresponding variable.If no \fIvarName\fR variables are specified, then \fBscan\fR works in aninline manner, returning the data that would otherwise be stored in thevariables as a list.  In the inline case, an empty string is returned whenthe end of the input string is reached before any conversions have beenperformed..SH "DETAILS ON SCANNING".PP\fBScan\fR operates by scanning \fIstring\fR and \fIformat\fR together.If the next character in \fIformat\fR is a blank or tab then itmatches any number of white space characters in \fIstring\fR (includingzero).Otherwise, if it isn't a \fB%\fR character then it must match the next character of \fIstring\fR.When a \fB%\fR is encountered in \fIformat\fR, it indicatesthe start of a conversion specifier..VS 8.4A conversion specifier contains up to four fields after the \fB%\fR:a \fB*\fR, which indicates that the converted value is to be discarded instead of assigned to a variable; a XPG3 position specifier; a numberindicating a maximum field width; a field size modifier; and aconversion character..VE 8.4All of these fields are optional except for the conversion character.The fields that are present must appear in the order given above..PPWhen \fBscan\fR finds a conversion specifier in \fIformat\fR, itfirst skips any white-space characters in \fIstring\fR (unless thespecifier is \fB[\fR or \fBc\fR).Then it converts the next input characters according to the conversion specifier and stores the result in the variable givenby the next argument to \fBscan\fR..PPIf the \fB%\fR is followed by a decimal number and a \fB$\fR, as in``\fB%2$d\fR'', then the variable to use is not taken from the nextsequential argument.  Instead, it is taken from the argument indicatedby the number, where 1 corresponds to the first \fIvarName\fR.  Ifthere are any positional specifiers in \fIformat\fR then all of thespecifiers must be positional.  Every \fIvarName\fR on the argumentlist must correspond to exactly one conversion specifier or an erroris generated, or in the inline case, any position can be specifiedat most once and the empty positions will be filled in with empty strings..PPThe following conversion characters are supported:.TP 10\fBd\fRThe input field must be a decimal integer.It is read in and the value is stored in the variable as a decimal string..VS 8.4If the \fBl\fR or \fBL\fR field size modifier is given, the scannedvalue will have an internal representation that is at least 64-bits insize..VE 8.4.TP 10\fBo\fRThe input field must be an octal integer. It is read in and the value is stored in the variable as a decimal string..VS 8.4If the \fBl\fR or \fBL\fR field size modifier is given, the scannedvalue will have an internal representation that is at least 64-bits insize.If the value exceeds MAX_INT (017777777777 on platforms using 32-bitintegers when the \fBl\fR and \fBL\fR modifiers are not given), itwill be truncated to a signed integer.  Hence, 037777777777 willappear as -1 on a 32-bit machine by default..VE 8.4.TP 10\fBx\fRThe input field must be a hexadecimal integer. It is read in and the value is stored in the variable as a decimal string..VS 8.4If the \fBl\fR or \fBL\fR field size modifier is given, the scannedvalue will have an internal representation that is at least 64-bits insize.If the value exceeds MAX_INT (0x7FFFFFFF on platforms using 32-bitintegers when the \fBl\fR and \fBL\fR modifiers are not given), itwill be truncated to a signed integer.  Hence, 0xFFFFFFFF will appearas -1 on a 32-bit machine..VE 8.4.TP 10\fBu\fRThe input field must be a decimal integer.  The value is stored in thevariable as an unsigned decimal integer string..VS 8.4If the \fBl\fR or \fBL\fR field size modifier is given, the scannedvalue will have an internal representation that is at least 64-bits insize..VE 8.4.TP 10\fBi\fR The input field must be an integer.  The base (i.e. decimal, octal, orhexadecimal) is determined in the same fashion as described in\fBexpr\fR.  The value is stored in the variable as a decimal string..VS 8.4If the \fBl\fR or \fBL\fR field size modifier is given, the scannedvalue will have an internal representation that is at least 64-bits insize..VE 8.4.TP 10\fBc\fRA single character is read in and its binary value is stored in the variable as a decimal string.Initial white space is not skipped in this case, so the inputfield may be a white-space character.This conversion is different from the ANSI standard in that theinput field always consists of a single character and no fieldwidth may be specified..TP 10\fBs\fRThe input field consists of all the characters up to the next white-space character; the characters are copied to the variable..TP 10\fBe\fR or \fBf\fR or \fBg\fRThe input field must be a floating-point number consisting of an optional sign, a string of decimal digits possiblycontaining a decimal point, and an optional exponent consisting of an \fBe\fR or \fBE\fR followed by an optional sign and a string of decimal digits.It is read in and stored in the variable as a floating-point string..TP 10\fB[\fIchars\fB]\fRThe input field consists of any number of characters in \fIchars\fR.The matching string is stored in the variable.If the first character between the brackets is a \fB]\fR thenit is treated as part of \fIchars\fR rather than the closingbracket for the set.If \fIchars\fRcontains a sequence of the form \fIa\fB\-\fIb\fR then anycharacter between \fIa\fR and \fIb\fR (inclusive) will match.If the first or last character between the brackets is a \fB\-\fR, thenit is treated as part of \fIchars\fR rather than indicating a range..TP 10\fB[^\fIchars\fB]\fRThe input field consists of any number of characters not in \fIchars\fR.The matching string is stored in the variable.If the character immediately following the \fB^\fR is a \fB]\fR then it is treated as part of the set rather than the closing bracket for the set.If \fIchars\fRcontains a sequence of the form \fIa\fB\-\fIb\fR then anycharacter between \fIa\fR and \fIb\fR (inclusive) will be excludedfrom the set.If the first or last character between the brackets is a \fB\-\fR, thenit is treated as part of \fIchars\fR rather than indicating a range..TP 10\fBn\fRNo input is consumed from the input string.  Instead, the total numberof characters scanned from the input string so far is stored in the variable..LPThe number of characters read from the input for a conversion is thelargest number that makes sense for that particular conversion (e.g.as many decimal digits as possible for \fB%d\fR, as many octal digits as possible for \fB%o\fR, and so on).The input field for a given conversion terminates either when awhite-space character is encountered or when the maximum field width has been reached, whichever comes first.If a \fB*\fR is present in the conversion specifier then no variable is assigned and the next scan argument is not consumed..SH "DIFFERENCES FROM ANSI SSCANF".PPThe behavior of the \fBscan\fR command is the same as the behavior ofthe ANSI C \fBsscanf\fR procedure except for the following differences:.IP [1]\fB%p\fR conversion specifier is not currently supported..IP [2]For \fB%c\fR conversions a single character value isconverted to a decimal string, which is then assigned to thecorresponding \fIvarName\fR;no field width may be specified for this conversion..IP [3].VS 8.4The \fBh\fR modifier is always ignored and the \fBl\fR and \fBL\fRmodifiers are ignored when converting real values (i.e. type\fBdouble\fR is used for the internal representation)..VE 8.4.IP [4]If the end of the input string is reached before any conversions have beenperformed and no variables are given, an empty string is returned..SH "SEE ALSO"format(n), sscanf(3).SH KEYWORDSconversion specifier, parse, scan

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?