Page 1 of 1

double expansion of variables

Posted: 12 Jul 2006 07:54
by dizze
Hello,

Is it possible to echo the value of a variable when the name of the variable is itself a variable.

I have (stripping other nonsence)

:loop
set cols=0
set /a c%cols%=%random%
set /a cols=%cols%+1
if cols==10 (goto :wherever) else (goto :loop)


And i want to be able to echo whatever %random% is which will be stored under whatever value is generate by c%cols%

This required (i think) a double expansion of a variable, but i cannot convince dos to do this.

Any ideas?

Posted: 15 Jul 2006 20:47
by DosItHelp
dizze,

Yes you can, in your case use:

Code: Select all

call echo.%%c%cols%%%

Before executing the line the command interpreter will resolve:

%cols% --> value of cols variable
%% ------> %


Assuming the value of the cols variable is 0 and the value of c0 is 12345 this means:

Code: Select all

call echo.%%c%cols%%%

will be resolved to:

Code: Select all

call echo.%c0%

Then the command line will be executed starting with call command which again will make the command interpreter resolve all variables first:
echo.12345

Hope this helps :wink:

Sucess!

Posted: 17 Jul 2006 09:39
by dizze
Thanks DosItHelp :D ,

That works perfectly, i can now get rid of my tempory file hack.

Dizz-E