arrayswindowsfor-loopbatch-filedelayedvariableexpansion

Batch - return value from inside a loop


I have the following Problem:

I have 2 functions/labels called STARTERand Get_age. The Get_age function stores ages and names in some related variables and shall return the age of the person i passed to it (passed the name). But the variable which shall store the return value -> means !arr[%%i].age!seems to be empty all the time. I think this is may of the (ENDLOCAL ...) block.

What confuses me too: if i only want to return the %%i inside the (ENDLOCAL ...) block it works fine, but when there are some !exclamation marks! the variable seems to get empty.

How can i now return the age easily without bloating the code? PS: This piece of code is only a small example to give a better view on it.

Thank´s a lot

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM FUNCTION WHICH ASKS STORES A NAME AND ASKS FOR ITS AGE
:STARTER
setlocal
set "person=Niels"
call :Get_age "%person%" "readback"
echo %person% is -%readback%- years old.
pause
exit /b 0

REM FUNCTION WHICH RETURN THE AGE TO THE GIVEN NAME
:Get_age
setlocal
set "searchName=%~1"
set "retVal=%~2"
set "arr[0].name=Niels"
set "arr[0].age=5"
set "arr[1].name=Julia"
set "arr[1].age=2"
set "arr[2].name=Claus"
set "arr[2].age=9"
set "arr_size=2"
REM Go through the arr and return the age to the given name
for /l %%i in (0 1 %arr_size%) do (
    if "!arr[%%i].name!" equ "%searchName%" (
        (ENDLOCAL
            set %retVal%=!arr[%%i].age!
        )
    )
)
exit /b 0

Solution

  • You could store the value and return it after the loop.

    set "found="
    for /l %%i in (0 1 %arr_size%) do (
        if "!arr[%%i].name!" equ "%searchName%" (
           set "found=!arr[%%i].age!"
        )
    )
    (
        ENDLOCAL
        set "%retVal%=%found%"
    )
    goto :eof