how to make an array by function and by local variable of the function

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

how to make an array by function and by local variable of the function

#1 Post by sincos2007 » 05 Apr 2019 05:57

Code: Select all

@echo off

set /a n=99

set ret=
call :test3 ret

echo show array:
echo %ret.Array[0]%
echo %ret.Array[1]%
echo %ret.Array[2]%
echo %ret.Array[3]%

ECHO Press any key to close the windows...
pause>NUL
goto :eof

:test3
setlocal
set /a n=0

:Loop-Start
if %n% GEQ 3 goto :Loop-End

set %~1.Array[%n%]=V%n%

set /a n=n+1

goto :Loop-Start
:Loop-End

endlocal
goto :eof

Hi,

I have written a function named test3 who make an array by its local variable named n(notice that there is a global variable named n). The function test3 returns the array to first parameter of it. But the result is incorrect

Thanks

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to make an array by function and by local variable of the function

#2 Post by aGerman » 05 Apr 2019 06:14

notice that there is a global variable named n
That gets overwritten with 0 in the scope of your function. And every variable you define inside of the function will be discarded because of the endlocal command at the end.
But the result is incorrect
What would be the result you expect?

Steffen

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: how to make an array by function and by local variable of the function

#3 Post by jeb » 05 Apr 2019 06:49

You need to preserve the values in the ENDLOCAL-Block.

Code: Select all

@echo off

set /a n=99

set ret=
call :test3 ret

echo show array:
echo %ret.Array[0]%
echo %ret.Array[1]%
echo %ret.Array[2]%
echo %ret.Array[3]%

goto :eof

:test3
setlocal EnableDelayedExpansion
set /a n=0

for /L %%n in (0 1 3) DO (
	set "%~1.Array[%%n]=V%%n"
)

REM *** Leaving the scopt, preverve the values 
set "_return="
for /L %%n in (0 1 3) DO (
	if defined _return (
		set "_return=!_return!&"
	)
	set "_return=!_return!set "%~1.Array[%%n]=!%~1.Array[%%n]!""
)

(
    endlocal
    %_return%
)
goto :eof


Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to make an array by function and by local variable of the function

#5 Post by Aacini » 05 Apr 2019 09:55

Code: Select all

@echo off

set /a n=99

call :test3 ret

echo show array:
echo %ret.Array[0]%
echo %ret.Array[1]%
echo %ret.Array[2]%
echo %ret.Array[3]%

ECHO Press any key to close the windows...
pause>NUL
goto :eof

:test3
setlocal

for /L %%i in (0,1,3) do set "%~1.Array[%%i]=V%%i"

set localEnv=1
for /F %%v in ('set %~1.Array[') do (
   if defined localEnv endlocal
   set "%%v"
)

goto :eof
Antonio

sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

Re: how to make an array by function and by local variable of the function

#6 Post by sincos2007 » 06 Apr 2019 02:28

Hi jeb,

This really works, just by preserve values in EndLocal block. Following is workable code for subroutine test3:

Code: Select all

:test3

setlocal EnableDelayedExpansion
set /a n=0

for /l %%i in (0,1,3) do (
   set s1=V!n!

   if defined _ret (
      set _ret=!_ret! ^& 
   )
   set _ret=!_ret!set %~1.Array[!n!]=V!n!

   set /a n=n+1
)

(
   endlocal
   %_ret%
)
goto :eof

Thanks.

Post Reply