That's very weird, I sincerelly went through all I know to find why, but didn't succeed.
I have these functions to emulate arrays, and they are working fine. Unfortunatelly, now I see the issue below. Better see the code to understand:
SCRIPT:
---------------------------------------------------------------------
@REM ============================================
@REM Main Script
@REM ============================================
@ECHO OFF
SETLOCAL
call:createArray TestArray
call:addElementToArray TestArray "amanhã vai ser bom"
call:addElementToArray TestArray "totler"
call:addElementToArray TestArray 1001
call:addElementToArray TestArray Ewerton
call:addElementToArray TestArray "Goddard"
call:addElementToArray TestArray "---------"
echo %TestArray.Length%
call:getArrayElement TestArray 2 Element2
call:getArrayElement TestArray 4 Element4
echo %Element2%
echo %Element4%
call:getArrayElement TestArray 3 Element2
call:getArrayElement TestArray 5 Element4
echo %Element2%
echo %Element4%
SET /A INDEX_LIMIT=%TestArray.Length%-1
FOR /L %%i IN (0,1,%INDEX_LIMIT%) DO (
CALL:getArrayElement TestArray %%i MyVariable
SET MyVariable
echo %MyVariable%
ECHO "=========================================================================="
)
ENDLOCAL
GOTO EXIT
@REM ============================================
@REM User Defined Functions
@REM ============================================
:getVarValue
SET "p_VarName=%~1"
SET "p_ReturnValue=%~2"
FOR /F "usebackq tokens=2* delims==" %%a IN (`set %p_VarName%`) DO (
SET "%p_ReturnValue%=%%a"
)
GOTO:EOF
@REM Array manipulation functions.
@REM Do not use array names that are composed by names of other variables (e.g., NewTestArray
:createArray
SET /A "%~1.Length=0"
GOTO:EOF
:getArrayLength
SET "p_ArrayName=%~1"
SET "p_ReturnValue=%~2"
call:getVarValue "%p_ArrayName%.Length" ArrayLength
SET "%p_ReturnValue%=%ArrayLength%"
GOTO:EOF
:addElementToArray
SET "p_ArrayName=%~1"
SET "p_ElementToAdd=%~2"
call:getArrayLength "%p_ArrayName%" ArrayLength
SET "ArrayPosition=%~1[%ArrayLength%]"
SET "%ArrayPosition%=%p_ElementToAdd%"
SET /A "%p_ArrayName%.Length=%ArrayLength%+1"
GOTO:EOF
:getArrayElement
SET "p_ArrayName=%~1"
SET "p_ElementPosition=%~2"
SET "p_ReturnValue=%~3"
call:getVarValue "%p_ArrayName%[%p_ElementPosition%]" %p_ReturnValue%
GOTO:EOF
@REM ============================================
@REM End Of Script
@REM ============================================
:EXIT
---------------------------------------------------------------------
OUTPUT:
---------------------------------------------------------------------
6
1001
Goddard
Ewerton
---------
MyVariable=amanhπ vai ser bom
---------
"=========================================================================="
MyVariable=totler
---------
"=========================================================================="
MyVariable=1001
---------
"=========================================================================="
MyVariable=Ewerton
---------
"=========================================================================="
MyVariable=Goddard
---------
"=========================================================================="
MyVariable=---------
---------
"=========================================================================="
---------------------------------------------------------------------
What could be causing "echo %MyVariable%" to don't show the right value (it's showing always "---------")?
Variable is set, but cannot print it with echo
Moderator: DosItHelp
Re: Variable is set, but cannot print it with echo
problem in line "echo %MyVariable%" - value of this variable change during FOR loopewerton wrote:What could be causing "echo %MyVariable%" to don't show the right value (it's showing always "---------")?
methot 1. use "SETLOCAL ENABLEDELAYEDEXPANSION" mode:
Code: Select all
echo !MyVariable!
methot 2. use CALL triks:
Code: Select all
call echo %%MyVariable%%
P.S. use "SETLOCAL /?" for HELP about EnableDelayedExpansion
Re: Variable is set, but cannot print it with echo
What can I say?
Simply amazing.
Thanks a lot for your help.
Simply amazing.
Thanks a lot for your help.