How to get value return from a function within a loop?

simon

I am trying to work out how to test the return value of a function call within a loop and breaks the loop if function returns a specific value.

The follow piece of batch script is my attempt, the function return expected value on the first 2 function calls. When the call is made within a loop, I cannot figure out how to test the return value. Please point me to a direction on how to test the function return value within the loop.

SET var1=2
SET var2=0
CALL :FUNC %var1 var2
ECHO var2 is: %var2%

REM ============================

SET var1=6
SET var2=0
CALL :FUNC %var1 var2
ECHO var2 is: %var2%

REM ============================

SETLOCAL EnableDelayedExpansion
SET list=1 0 3 4
FOR %%n IN (%list%) DO (
    CALL :FUNC %%n rtn
    IF !rtn! == 0 (
        GOTO DONE
    )
)
ECHO rtn is: %rtn%

: DONE
PAUSE
GOTO :eof


REM %1 is an in parameter
REM %2 is an out parameter
: FUNC
SETLOCAL EnableDelayedExpansion
SET var=%1
EndLocal & SET %2=%var% & GOTO :eof
jeb

I can't see any problems with your loop, but with your function.
You only need to change the last line to (the quotes are new):
EndLocal & SET "%2=%var%" & GOTO :eof

Without the quotes, you always appended a single space to your return value,
therefore the IF !rtn! == 0 ( was always false

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to return value from for loop in .then function in Cypress

Getting return value from function within get request

How to return the value of function 2 from within function 1

How to get a return value from a connect function

How to get return value from stub function?

How to get the return type of a member function from within a class?

Java return value only from within a loop

How to get a vector/array output from a loop within a function?

(Swift) Confused between a return value of a for-in loop within a function, and a return value that comes after the loop (within the function))

Get the return value of a single function within Promise

How to get value from a react function within a react component

How to return a value within a function with an event listener?

How to return the value given by the gps within a function

How to call stored procedure within function by output value, then get and return this value?

Get return value of function in a loop - shell script

How to get count value increamented within loop

How to return a value from a function, which is generated inside a callback function within?

Return value from a function callback in a for loop

String return value does not get assigned within for-loop in Rust

How to get a return value from a React component function prop?

How to get function return value from a multiprocessing.Process?

How to get the value from the return function and send it to the html element

How to get a return value from async function to a variable in React Native?

How to get the single return value from mysql stored function in php

How to get the variable value from the return of a function in R

How to properly get a return value from a PHP function?

Pika: How to get a return value from a callback function?

How to get a return value from the function loaded in CreateThread()?

How to get a return value from a function called during an event?