In the following script I want to pass a string via variable and the variable name for an array which should contain substrings to a subroutine.
The subroutine puts substrings of the passed string into an array/list which then should get "returned" by setting it as the value of the 2. passed parameter.
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET testString=Hello World
REM Pass testString and substrings to subroutine
CALL :get_substrings testString substrings
REM For testing. Echo substrings. DOESN'T WORK. substrings is empty!
FOR /L %%s IN (0,1,2) DO (
ECHO !substrings[%%s]!
)
ENDLOCAL
EXIT /B 0
:get_substrings
SETLOCAL ENABLEDELAYEDEXPANSION
SET "string=!%~1!"
REM Alternative approach: Make a connection to %2 rightaway
REM SET "substrings=!%~2!"
REM Process string: Put substrings into indexed array. This works as expected!
FOR /L %%s IN (0,1,2) DO (
SET substrings[%%s]=!string:~0,5!
SET string=!string:~5!
)
REM For testing. Echo the substrings. Works as expected!
FOR /L %%s IN (0,1,2) DO (
ECHO !substrings[%%s]!
)
REM For alternative approach
REM ENDLOCAL
REM End the local the set 2.param = substringsArray
ENDLOCAL & SET %2=%substrings%
EXIT /B 0
Processing the string by creating a array with substrings in the subroutine works as expected. But setting 2. parameters value and keeping the value after subroutine doesn't work...
Notes: The processing of the string is just a dummy. The real process is slightly different but the core with the substrings array is the same. The script is executable right away.
So, how can I get the value substrings back?