![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-02 14:17 |
48,038 topics / 350,123 posts / today 1 new / 48,251 members |
| DOS批处理 & 脚本技术(批处理室) » Regarding the problem of variable reference |
| Printable Version 1,779 / 13 |
| Floor1 #four# | Posted 2006-11-21 04:36 |
| 中级用户 Posts 34 Credits 209 | |
|
I was learning the set command and used the following batch script to test the problem of variable reference, but the result could not be displayed correctly. Please help me solve it.
The batch script is as follows: @echo off set num=iamyxj set a=0 set /a a+=1 echo %num:~%a%,1% echo %num:~1,1% pause I want to use the reference of variable a to display any character of variable num. I found that variable %a% cannot be parsed. Even if I set the variable directly to 1, it cannot be displayed correctly, but the following sentence can display the first character normally. I can't figure it out. Hope the expert can explain. |
|
| Floor2 namejm | Posted 2006-11-21 04:47 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
This belongs to the problem of variable delay. You can change it to the following code:
For more information, please refer to: Under what circumstances should variable delay be used? |
|
| Floor3 #four# | Posted 2006-11-21 05:00 |
| 中级用户 Posts 34 Credits 209 | |
|
I read the article about variable delay. Brother willsort said like this.
The behavior of the command interpreter expanding environment variables is roughly as follows: First, read a complete statement of the command line. After some preliminary preprocessing, before the command is interpreted and executed, it will match the string enclosed by percent signs. If a matching environment variable is found in the environment space, its value will replace the original string and the percent sign itself. If no match is obtained, an empty string will replace it. This process is the "expansion" of the environment variable, which still belongs to the preprocessing category of the command line. But the batch processing I mentioned above is not a single statement. I first set the variables nul and a, and then when reading the next sentence, should I be able to find this variable from the environment space? Why do I also need to use the! number to delay referencing this variable? Can the moderator give us some more examples for newbies? |
|
| Floor4 ccwan | Posted 2006-11-21 05:04 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
![]() |
|
| Floor5 tao0610 | Posted 2006-11-21 05:14 |
| 高级用户 Posts 218 Credits 579 | |
This will match the string enclosed by percent signs. This will match the part between the first two %% first, which is %num:~%! |
|
| Floor6 不得不爱 | Posted 2006-11-21 05:18 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
|
Because you are using variable nesting variables, you must use!, and when using!, you must enable delayed variables.
|
|
| Floor7 9527 | Posted 2006-11-21 05:21 |
| 银牌会员 Posts 438 Credits 1,185 From 北京 | |
|
Actually, besides the method of brother NAMEJM on the second floor, you can also use the internal command CALL
@echo off set num=iamyxj set a=0 set /a a+=1 call echo %%num:~%a%,1%% This form can be okay echo %num:~1,1% pause |
|
| Floor8 不得不爱 | Posted 2006-11-21 05:53 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
|
The person upstairs is also unable to use it.
|
|
| Floor9 9527 | Posted 2006-11-21 06:20 |
| 银牌会员 Posts 438 Credits 1,185 From 北京 | |
|
I think the person upstairs should first ask for the test results of others before speaking.
|
|
| Floor10 lxmxn | Posted 2006-11-21 14:25 |
| 版主 Posts 4,938 Credits 11,386 | |
|
After testing, Brother 9527's code on floor 7 is correct and gets the correct result. The use of call is clever~ Learned it~ |
|
| Floor11 tao0610 | Posted 2006-11-21 14:58 |
| 高级用户 Posts 218 Credits 579 | |
|
```
@echo on&setlocal enabledelayedexpansion set num=iamyxj set a=12345 set b=0 set /a b+=1 call echo %%num:~!a:~%b%,1!,1%% echo %num:~1,1% pause ``` Both can be used to apply variable twice. |
|
| Floor12 amao | Posted 2006-11-21 19:13 |
| 中级用户 Posts 152 Credits 316 | |
|
Just started learning batch processing under CMD, keep it for later use.
|
|
| Floor13 redtek | Posted 2006-11-21 22:42 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
Multiple variable nested test experiment, version: Yunyun version ~:)
The test output value results are as follows: The above experimental variable nesting requires 4 "actions" to completely "transform" and display the actual desired value ~:) Simple principle explanation: The character value taken from variable num is determined by another variable b, and the orientation of a certain character value in variable b is determined by variable a again. Rule: %% two percent signs are used for output, which represents one % sign, If you want to output %% two percent signs, you need to use four percent signs. Using the above principle, use different quantities of % percent signs to determine the level of variable nesting, The more levels, the double use quantity of percent signs also increases accordingly, and this principle can be used to complete extremely complex nesting. Disadvantage: The more complex the nesting, the more difficult it is to read clearly, and the more difficult it is to maintain the code. So, another advantage of using delayed variables is that the code can be very clear ~:) [ Last edited by redtek on 2006-11-21 at 09:53 AM ] |
|
| Floor14 不得不爱 | Posted 2006-11-21 23:03 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
|
After testing, Brother 9527's code on the 7th floor is correct and the correct result is obtained. Add 5 points
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |