China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-02 12:51
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Regarding the problem of variable reference View 1,778 Replies 13
Original Poster Posted 2006-11-21 04:36 ·  中国 浙江 杭州 电信
中级用户
Credits 209
Posts 34
Joined 2004-05-25 00:00
22-year member
UID 25296
Gender Male
Status Offline
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.
Floor 2 Posted 2006-11-21 04:47 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
This belongs to the problem of variable delay. You can change it to the following code:


@echo off
setlocal enabledelayedexpansion
set num=iamyxj
set a=0
set /a a+=1
echo !num:~%a%,1!
echo %num:~1,1%
pause


For more information, please refer to: Under what circumstances should variable delay be used?
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2006-11-21 05:00 ·  中国 浙江 杭州 电信
中级用户
Credits 209
Posts 34
Joined 2004-05-25 00:00
22-year member
UID 25296
Gender Male
Status Offline
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?
Floor 4 Posted 2006-11-21 05:04 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 5 Posted 2006-11-21 05:14 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline

echo %num:~%a%,1%


This will match the string enclosed by percent signs. This will match the part between the first two %% first, which is %num:~%!

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 6 Posted 2006-11-21 05:18 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Because you are using variable nesting variables, you must use!, and when using!, you must enable delayed variables.
Floor 7 Posted 2006-11-21 05:21 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
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
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
不得不爱 +5 2006-11-21 23:02
我今后在论坛的目标就是做个超级坏人!!!
Floor 8 Posted 2006-11-21 05:53 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 9 Posted 2006-11-21 06:20 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
I think the person upstairs should first ask for the test results of others before speaking.
我今后在论坛的目标就是做个超级坏人!!!
Floor 10 Posted 2006-11-21 14:25 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

  After testing, Brother 9527's code on floor 7 is correct and gets the correct result.
  
  The use of call is clever~ Learned it~
Floor 11 Posted 2006-11-21 14:58 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
```
@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.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
namejm +2 2006-11-21 22:45

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 12 Posted 2006-11-21 19:13 ·  美国 北达科他州立大学
中级用户
★★
Credits 316
Posts 152
Joined 2006-06-18 13:01
20-year member
UID 57204
Gender Male
Status Offline
Just started learning batch processing under CMD, keep it for later use.
Floor 13 Posted 2006-11-21 22:42 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Multiple variable nested test experiment, version: Yunyun version ~:)


@echo off
cls

set num=Redtek 2006 bbs.cn-dos.net
set a=12345
set b=654321

echo ● Variable test:
echo.
echo ● Want to get ^%num:~5,1^%, that is: take the value of variable num starting from offset 5, take 1 character, the result: k
echo   According to the above principle, test variable nested variable value taking without using delayed variables.
echo.
echo ● Now we set that the ~5 in ^%num:~5,1^% should come from another variable b indirectly.
echo   So, ^%num:~5,1^% should (pseudocode) be expressed as: ^%num:~b,1^%, but there are multiple values in variable b,
echo   We only want to take the first element from offset 0 in variable b, which is 5. We have started nested variables.
echo   So we need to nest the statement to get variable b (^%b:~1,1^%) in the variable value taking statement of num again.
echo.
echo   That is: taking a certain character in variable num should be determined by another variable b from where to start taking.
echo     But, we also need to nest variables, that is:
echo     The character value in variable b that determines which character in variable num to take is determined, so we need to decompose b again,
echo     In this way, it is a three-level nested variable relationship, and they influence and nest variables layer by layer, haha...
echo.
echo ● So, we use multi-level test to display the whole process of value taking evolution ~:)
echo.
echo   The following are the results of four "asymptotic" evaluation processes, and finally it takes 3 Calls to find out these nested variables ~:)
echo.

echo   %%%%%%%%num:~%%%%b:~%a:~0,1%,1%%%%,1%%%%%%%%

call echo   %%%%%%%%num:~%%%%b:~%a:~0,1%,1%%%%,1%%%%%%%%

call call echo   %%%%%%%%num:~%%%%b:~%a:~0,1%,1%%%%,1%%%%%%%%

call call call echo   The final value is: %%%%%%%%num:~%%%%b:~%a:~0,1%,1%%%%,1%%%%%%%%


rem In theory, we can Call it n times, so we can nest variables randomly in variables ~:)
rem The above is an experimental process of "Yunyun" to have a deeper understanding of nested variables ~:)




The test output value results are as follows:

  %%%%num:~%%b:~1,1%%,1%%%%
  %%num:~%b:~1,1%,1%%
  %num:~5,1%
  The final value is: k


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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
namejm +2 2006-11-21 22:55
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 14 Posted 2006-11-21 23:03 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
After testing, Brother 9527's code on the 7th floor is correct and the correct result is obtained. Add 5 points
Forum Jump: