批量
在下面的代码中,我创建了一些伪数组(表示对)并调用一个例程,该例程获取伪数组的名称及其大小作为参数值。 在例程中,我想访问循环中的每个数组元素。 据我所知,访问数组值的唯一可能是使用延迟扩展,如果我可以取回延迟扩展本地的值,这将不会有问题。
@ECHO OFF SETLOCAL DISABLEDELAYEDEXPASIO set pairs[0]="a" "b" set pairs[1]="c" "d" set pairs[2]="e" "f" set pairumber= call :my_routine pairs %pairumber% EDLOCAL EXIT /B 0 :my_routine SETLOCAL DISABLEDELAYEDEXPASIO set myPairs=%1 set /a maxCount=%2-1 for /l %%c in (0,1,%maxCount%) do ( set "currentPair=" SETLOCAL EABLEDELAYEDEXPASIO call set "pair=%%!myPairs![%%c]%%" REM setting the value works: REM echo !pair! EDLOCAL & set "currentPair=%pair%" echo %currentPair% ) EDLOCAL exit /b 0预期产出:
"a" "b" "c" "d" "e" "f"关键部分是& set currentPair=%pair% 。 据我所知,由于在设置时使用了延迟扩展,因此未设置%pair%值。
问题是关于获得价值而不是告诉我我可以在当地本身做事。 如果无法获得价值,我会考虑这一点。
我也看了一下这个非常类似的问题,但解决方案对我不起作用。
所以...
如何从内部局部获取值,以便currentPair获得对的值?
In the following code I create some pseudo array (representing pairs) and call a routine which gets the name of the pseudo array and its size as parameter values. In the routine I want to access each array element in a loop. To my knowledge the only possibility to access the array values is to use delayed expansion which would not be problem if i could get back the value of the delayed expansion local.
@ECHO OFF SETLOCAL DISABLEDELAYEDEXPASIO set pairs[0]="a" "b" set pairs[1]="c" "d" set pairs[2]="e" "f" set pairumber= call :my_routine pairs %pairumber% EDLOCAL EXIT /B 0 :my_routine SETLOCAL DISABLEDELAYEDEXPASIO set myPairs=%1 set /a maxCount=%2-1 for /l %%c in (0,1,%maxCount%) do ( set "currentPair=" SETLOCAL EABLEDELAYEDEXPASIO call set "pair=%%!myPairs![%%c]%%" REM setting the value works: REM echo !pair! EDLOCAL & set "currentPair=%pair%" echo %currentPair% ) EDLOCAL exit /b 0Expected Output:
"a" "b" "c" "d" "e" "f"The crucial part is & set currentPair=%pair%. To my understanding the %pair% value is not set because of using delayed expansion when setting it.
The question is about getting the value back OT about telling me that I could do stuff in the local itself. This is something i would cider if there is no way to get the value back.
I also took a look at this very similiar question but the solution does not work for me.
So...
How do i get the value out of the inner local so that currentPair gets the value of pair?
最满意答案
将变量值传递到endlocal barrier之外的常用方法依赖于它不是放在代码块中的事实,而是放置了endlocal & set "currentPair=%pair%"行endlocal & set "currentPair=%pair%"在for /L循环中endlocal & set "currentPair=%pair%" 。
您可以使用for /F循环,如下所示:
for /F "delims=" %%E in (""!pair!"") do ( endlocal set "currentPair=%%~E" )外面的一对报价!pair! 告诉for /F它是一个文字字符串; 内部对是该字符串的一部分,然后由%%~E的~修饰符删除。 这样做的目的是确保循环总是迭代一次(即使!pair!为空),因此endlocal会被执行。
这也适用于另一个代码块,如for /L循环。
当然,行echo %currentPair%不返回正确的字符串,但你可以放置echo(%%~E代替for /F循环)。
The usual way of passing a variable value beyond the endlocal barrier relies on the fact that it is not placed within a block of code, but you placed the line endlocal & set "currentPair=%pair%" within a for /L loop.
You can use a for /F loop instead, like this:
for /F "delims=" %%E in (""!pair!"") do ( endlocal set "currentPair=%%~E" )The outer pair of quotes around !pair! tells for /F that it is a literal string; the inner pair is part of that string, which is then removed by the ~ modifier in %%~E. The purpose of this is to ensure that the loop always iterates once (even when !pair! is empty), and so endlocal becomes executed.
This also works in another block of code like your for /L loop.
Of course the line echo %currentPair% does not return the correct string, but you could place echo(%%~E within the for /F loop instead.
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 11 条评论) |
本站网友 抱团发展 | 12分钟前 发表 |
您可以使用for /F循环 | |
本站网友 分生孢子 | 29分钟前 发表 |
因此endlocal会被执行 | |
本站网友 去疤美白 | 16分钟前 发表 |
以便currentPair获得对的值? In the following code I create some pseudo array (representing pairs) and call a routine which gets the name of the pseudo array and its size as parameter values. In the routine I want to access each array element in a loop. To my knowledge the only possibility to access the array values is to use delayed expansion which would not be problem if i could get back the value of the delayed expansion local. @ECHO OFF SETLOCAL DISABLEDELAYEDEXPASIO set pairs[0]="a" "b" set pairs[1]="c" "d" set pairs[2]="e" "f" set pairumber= call | |
本站网友 燕窝的功效与作用 | 11分钟前 发表 |
但解决方案对我不起作用 | |
本站网友 看什么看 | 15分钟前 发表 |
因此endlocal会被执行 | |
本站网友 糊精是什么 | 18分钟前 发表 |
我创建了一些伪数组(表示对)并调用一个例程 | |
本站网友 西安房屋出租网 | 18分钟前 发表 |
and so endlocal becomes executed. This also works in another block of code like your for /L loop. Of course the line echo %currentPair% does not return the correct string | |
本站网友 多媒体教室软件 | 15分钟前 发表 |
如下所示: for /F "delims=" %%E in (""!pair!"") do ( endlocal set "currentPair=%%~E" ) 外面的一对报价!pair! 告诉for /F它是一个文字字符串; 内部对是该字符串的一部分 | |
本站网友 女贞子的功效 | 10分钟前 发表 |
以便currentPair获得对的值? In the following code I create some pseudo array (representing pairs) and call a routine which gets the name of the pseudo array and its size as parameter values. In the routine I want to access each array element in a loop. To my knowledge the only possibility to access the array values is to use delayed expansion which would not be problem if i could get back the value of the delayed expansion local. @ECHO OFF SETLOCAL DISABLEDELAYEDEXPASIO set pairs[0]="a" "b" set pairs[1]="c" "d" set pairs[2]="e" "f" set pairumber= call | |
本站网友 你是我今生该等的人 | 4分钟前 发表 |
my_routine pairs %pairumber% EDLOCAL EXIT /B 0 |