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 DISABLEDELAYEDEXPANSION
set pairs[0]="a" "b"
set pairs[1]="c" "d"
set pairs[2]="e" "f"
set pairNumber=3
call :my_routine pairs %pairNumber%
ENDLOCAL
EXIT /B 0
:my_routine
SETLOCAL DISABLEDELAYEDEXPANSION
set myPairs=%1
set /a maxCount=%2-1
for /l %%c in (0,1,%maxCount%) do (
set "currentPair="
SETLOCAL ENABLEDELAYEDEXPANSION
call set "pair=%%!myPairs![%%c]%%"
REM setting the value works:
REM echo !pair!
ENDLOCAL & set "currentPair=%pair%"
echo %currentPair%
)
ENDLOCAL
exit /b 0
Expected 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 NOT about telling me that I could do stuff in the local itself. This is something i would consider 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
?