Hi,
I am working on a script to get max lengths of each column, I'm trying to store lengths of max length in _c1...n vars. number of columns unknown.
I was able to get length for each column, create variables to store each with set _c!i! = !n!, n is the length
but in order to set the max length for a particular column I need to compare current with max and use something like !_c!!i!! which doesn't work, any ideas how to refer a variable which part of it's name coming from another variable?
Thanks...
nested variable name using EnableDelayedExpansion
Moderator: DosItHelp
Re: nested variable name using EnableDelayedExpansion
There are no error messages, here the code snippet I am using to test this
@echo off & setlocal EnableDelayedExpansion
cls
set a=1
set b=2
set _c!a!=3
set _c!b!=5
echo a : !a! - b : !b! - set_c? is below
set _c
echo trying to display _c s, (this works)
for %%i in (1,2) do echo !_c%%i!
echo this doesn't work
echo : !_c%%a!
exit
The output is
a : 1 - b : 2 - set_c? is below
_c1=3
_c2=5
trying to display _c s, (this works)
3
5
this doesn't work
:
The code highlighted in red is not working last line not displaying anything, I need to be able to refer _c1, _c2 using a parameter.
Thanks!
@echo off & setlocal EnableDelayedExpansion
cls
set a=1
set b=2
set _c!a!=3
set _c!b!=5
echo a : !a! - b : !b! - set_c? is below
set _c
echo trying to display _c s, (this works)
for %%i in (1,2) do echo !_c%%i!
echo this doesn't work
echo : !_c%%a!
exit
The output is
a : 1 - b : 2 - set_c? is below
_c1=3
_c2=5
trying to display _c s, (this works)
3
5
this doesn't work
:
The code highlighted in red is not working last line not displaying anything, I need to be able to refer _c1, _c2 using a parameter.
Thanks!
Re: nested variable name using EnableDelayedExpansion
anaforft wrote:Code: Select all
echo this doesn't work
echo : !_c%%a!
Should work if you fix the syntax.
Code: Select all
echo : !_c%a%!
That said, the posted code doesn't quite match the question you appeared to be asking originally.
Liviu
Re: nested variable name using EnableDelayedExpansion
It works. Thanks.
Re: nested variable name using EnableDelayedExpansion
Liviu wrote:anaforft wrote:Code: Select all
echo this doesn't work
echo : !_c%%a!
Should work if you fix the syntax.Code: Select all
echo : !_c%a%!
That said, the posted code doesn't quite match the question you appeared to be asking originally.
Liviu
One of my main reasons why I use descriptive variable names. To easy to screw up the syntax from a FOR LOOP token and a regular variable name.
Re: nested variable name using EnableDelayedExpansion
When the line is parsed %a% is first expanded to its value, in your case 1. So the line becomes: echo _c1 = !_c1!
Then, when the line is executed, and because of enableDelayedExpansion, !_c1! is expanded to its value, in your case 3, so what's run in the end is: echo _c1 = 3
Liviu
Then, when the line is executed, and because of enableDelayedExpansion, !_c1! is expanded to its value, in your case 3, so what's run in the end is: echo _c1 = 3
Liviu
Re: nested variable name using EnableDelayedExpansion
I agree in that,Squashman wrote:One of my main reasons why I use descriptive variable names. To easy to screw up the syntax from a FOR LOOP token and a regular variable name.
Longer variable names are a good choice when the amount of variables is low,
They'll make themselves more clear at the cost of obfuscating the algorithm.
Shorter variable names are a good choice when the amount of variables is high,
They'll make the algorithm more clear at the cost of obfuscating themselves.