『第 6 楼』:
有区别
使用 LLM 解释/回答一下
for的普通应用中,%a与%i、%k并无实质区别,指代一组文件、目录或数字序列只用一个变量就够用了,for语句中不会同时出现多个变量符号。
但使用/f参数解析文件组时,"options"参数里的delims和tokens关键字定义了多组字符,必须要用多个变量来依次代表,于是我们经常看到for /f语句中存在多个变量符号,这时是使用%a还是%i就存在区别了。
先看一段关于for /f 变量的帮助信息:
“%i 专门在 for 语句中得到说明,%j 和 %k 是通过tokens= 选项专门得到说明的。您可以通过 tokens= 一行指定最多 26 个符号,只要不试图说明一个高于字母 'z' 或'Z' 的变量。请记住,FOR 变量是单一字母、分大小写和全局的;而且,同时不能有 52 个以上都在使用中。”
这里讲了,只能定义26个符号,也就是for /f语句中最多只能出现26个变量符号。如果第一个变量定义为%a,第二个就是%b,依此类推,最后一个就是z%,变量的总数是26个;而把第一个变量定义为%i,第二个就只能是j%,最后一个仍是z%,不过可定义变量的数量少了8个,只剩18个了。
下面的例子能直观的看出区别:
如果3.txt文件的格式如下:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
那么for /f "tokens=1-30 delims=," %a in (3.txt) do echo a%,%z看到的是1,26,而for /f "tokens=1-30 delims=," %i in (3.txt) do echo i%,%z看到则是1,18(18以后的数字没法定义了)。
Last edited by hizebra on 2007-10-11 at 05:53 PM ]
In ordinary applications of for, %a, %i, and %k are not essentially different. Only one variable is sufficient to refer to a group of files, directories, or number sequences, and multiple variable symbols will not appear simultaneously in the for statement.
But when parsing file groups using the /f parameter, the delims and tokens keywords in the "options" parameter define multiple groups of characters, and multiple variables must be used to represent them in sequence. So we often see multiple variable symbols in the for /f statement, and then there is a difference between using %a or %i.
First, let's look at a piece of help information about for /f variables:
"%i is specifically explained in the for statement, and %j and %k are specifically explained through the tokens= option. You can specify up to 26 symbols in a tokens= line, as long as you don't try to explain a variable higher than the letter 'z' or 'Z'. Remember that FOR variables are single letters, case-sensitive, and global; and at the same time, no more than 52 can be in use."
Here it is mentioned that only 26 symbols can be defined, that is, at most 26 variable symbols can appear in the for /f statement. If the first variable is defined as %a, the second is %b, and so on, the last one is %z, and the total number of variables is 26; while if the first variable is defined as %i, the second can only be %j, and the last one is still %z, but the number of definable variables is reduced by 8, leaving only 18.
The following example can intuitively show the difference:
If the format of the 3.txt file is as follows:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
Then for /f "tokens=1-30 delims=," %a in (3.txt) do echo a%,%z will see 1,26, while for /f "tokens=1-30 delims=," %i in (3.txt) do echo i%,%z will see 1,18 (numbers after 18 cannot be defined).
Last edited by hizebra on 2007-10-11 at 05:53 PM ]
|