多个变量相互嵌套测试实验,版本:晕晕版~:)
@echo off
cls
set num=Redtek 2006 bbs.cn-dos.net
set a=12345
set b=654321
echo ● 变量测试:
echo.
echo ● 想取 ^%num:~5,1^% ,即:从偏移量5开始取变量num的值,取1个字符,结果:k
echo 根据上面原理,在不使用延时变量的情况下测试变量嵌套变量取值。
echo.
echo ● 现在我们设^%num:~5,1^%这里面的~5要求间接来自另一个变量b。
echo 所以,^%num:~5,1^%应(伪代码)表示为:^%num:~b,1^%,但变量b内有多个值,
echo 我们只想取变量b内的从偏移量0起的第1个元素,就是5。已经开始嵌套变量了。
echo 所以我们还要在num的变量取值计算语句中再嵌套进取变量b(^%b:~1,1^%)的语句。
echo.
echo 即:取变量num中的某个字符要由另一个变量b来决定从哪里开始取。
echo 但是,我们还要让它再嵌套变量,即:
echo 决定取num变量中的哪一个字符由变量b中某个字符值决定,所以还要再分解b,
echo 这样,就是三层嵌套变量的关系,它们层层相互影响并嵌套变量着,哈哈……
echo.
echo ● 所以,我们使用了多级测试显示取值的整个过程的演化~:)
echo.
echo 下面结果是四条“渐近”求值的过程,最后连Call了3次才求出这些被嵌套的变量:)
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 最后值为:%%%%%%%%num:~%%%%b:~%a:~0,1%,1%%%%,1%%%%%%%%
rem 理论上,我们可以Call它n多次,这样我们可以随便在变量中再嵌套变量:)
rem 以上是一个对嵌套变量实现更深了解的“晕晕”的实验过程~:)
测试输出的值的结果如下:
%%%%num:~%%b:~1,1%%,1%%%%
%%num:~%b:~1,1%,1%%
%num:~5,1%
最后值为:k
以上实验变量嵌套需要 4 次“动作”才能完全“转化”并显示实际希望值:)
简单的说明原理: 从变量num中取的字符值,由另一个变量b决定,而变量b中某个字符值的取向再由变量a来决定。
规律: %%两个百分号用于输出的话,就代表一个%号,
如果想输出%%两个百分号,就要用4个百分号。
利用上面原理,使用%百分号的不同的数量来决定变量嵌套的级数,
级数越多,百分号的双倍使用数量也随着递增,使用这个原理可以完成极复杂的嵌套。
缺点:越复杂的嵌套几乎越是无法清晰读懂,同时越难维护代码。
所以,使用延时变量另一个好处在于可以让代码非常清晰:)
Last edited by redtek on 2006-11-21 at 09:53 AM ]
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 ]