批量递归动态变量的返回值 [英] Return Values of Recursive Dynamic Variables in Batch
问题描述
我正在尝试使用延迟变量扩展"中的动态变量来表示其他动态变量.我遇到了麻烦.如果该动态变量的值是另一个具有其自身值的动态变量,如何获得该动态变量的值?
I'm attempting to use dynamic variables within Delayed Variable Expansion to represent other Dynamic Variables. I'm running into some trouble. How can I get a dynamic variable's value's value if the dynamic variable's value is another dynamic variable with it's own value?
即!valA! =%valB%=此
i.e. !valA! = %valB% = this
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
...
...
FOR /F ... %%G IN (...) DO (
SET _temp=%%~nG
SET _file=!_temp:~0,-4!
SET _cnt=0
FOR /F ... %%L IN (...) DO (
SET _temp=%%L
SET _str=!_temp:*: =!
SET /A _cnt+=1
SET _temp=x!_file!!_cnt!
IF DEFINED !_temp! (
SET _temp=!%_temp%!
::
::_temp('s value) is _var('s value) is "xyz"
::Set new _temp to equal current _temp's "xyz"
::
IF !_temp! NEQ !_str! (
ECHO File Content Mismatch
)
) ELSE (
SET xvar=!_temp!
SET !xvar!=!_str!
)
)
)
...
...
exit
任何帮助将不胜感激.
推荐答案
逻辑上,您失败的代码等同于
Logically your failing code equates to
setlocal enableDelayedExpansion
(
set VAR1=success
set VAR2=VAR1
set VAR3=!%VAR2%!
echo VAR3=!VAR3!
)
正如前面的答案和注释中已经指出的那样,您无法为VAR2分配值,然后无法在同一代码块内使用%VAR2%访问该值,因为%VAR2%在代码块,此时的值不是您想要的值(可能是未定义的).
As has already been pointed out in previous answers and comments, you can't assign a value to VAR2 and then access the value using %VAR2% within the same code block because %VAR2% is expanded during the parsing phase of the code block, at which point the value is not what you want (probably undefined).
但是,除了!VAR2之外,您还需要第二级扩展!得到想要的结果.我知道三种解决方案.
But you need a second level of expansion in addition to !VAR2! to get the result you want. I know of three solutions.
1),该解决方案有效,但不建议这样做,因为它很慢.
1) This solution works, but it is not recommended because it is slow.
setlocal enableDelayedExpansion
(
set VAR1=success
set VAR2=VAR1
call set VAR3=%%!VAR2!%%
echo VAR3=!VAR3!
)
在执行CALL之前,每个%%都会减少为%,然后是!VAR2!变成VAR1.
因此,被调用的语句变为set VAR3=%VAR1%
,并且在%var%扩展阶段重新解析了被调用的语句,因此您将获得所需的结果.
Prior to the execution of the CALL, each %% is reduced to %, and !VAR2! becomes VAR1.
Thus the called statement becomes set VAR3=%VAR1%
, and the called statement is reparsed through the %var% expansion phase, so you get your desired result.
但是-通话相对来说非常昂贵.在循环中使用时,可能会导致严重的性能下降. jeb在打电话给我,或者最好避免致电
BUT - CALL is relatively very expensive. When used in a loop it can cause severe performance degradation. jeb provides a good demonstration and explanation at CALL me, or better avoid call
1a)有一个CALL解决方案的变体,您可以调用:LABELed子例程.因为子例程是在调用之后解析的,所以您可以简单地使用set VAR3=!%VAR2%!
.但这又使用了CALL,因此速度相对较慢,不建议使用.
1a) There is a variation of the CALL solution where you call a :LABELed subroutine. Because the subroutine is parsed after the call, you can simply use set VAR3=!%VAR2%!
. But again, this uses CALL so it is relatively slow and not recommended.
2)此通用解决方案有效,相比而言, MUCH 更快.它使用FOR变量进行第二级扩展.这是推荐的解决方案.
2) This general solution works, and is MUCH faster in comparison. It uses a FOR variable for the second level of expansion. This is the recommended solution.
setlocal enableDelayedExpansion
(
set VAR1=success
set VAR2=VAR1
for /f %%A in ("!VAR2!") do set VAR3=!%%A!
echo VAR3=!VAR3!
)
3)如果已知VAR1的值为整数,则存在使用SET /A
3) If the value of VAR1 is known to be an integer, then there is a special case solution using SET /A
setlocal enableDelayedExpansion
(
set VAR1=999
set VAR2=VAR1
set /a VAR3=!VAR2!
echo VAR3=!VAR3!
)
SET/A命令具有其自己的内置变量扩展,该扩展在延迟扩展之后发生,不需要标点符号.
The SET /A command has its own built in expansion of variables that takes place after delayed expansion, and no punctuation is needed.
这篇关于批量递归动态变量的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!