nested variable name using EnableDelayedExpansion

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
anaforft
Posts: 3
Joined: 27 Aug 2012 15:23

nested variable name using EnableDelayedExpansion

#1 Post by anaforft » 27 Aug 2012 15:31

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...

anaforft
Posts: 3
Joined: 27 Aug 2012 15:23

Re: nested variable name using EnableDelayedExpansion

#2 Post by anaforft » 27 Aug 2012 18:24

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!

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: nested variable name using EnableDelayedExpansion

#3 Post by Liviu » 27 Aug 2012 19:21

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

anaforft
Posts: 3
Joined: 27 Aug 2012 15:23

Re: nested variable name using EnableDelayedExpansion

#4 Post by anaforft » 27 Aug 2012 19:40

It works. Thanks.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: nested variable name using EnableDelayedExpansion

#5 Post by Squashman » 27 Aug 2012 20:10

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.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: nested variable name using EnableDelayedExpansion

#6 Post by Liviu » 27 Aug 2012 21:47

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: nested variable name using EnableDelayedExpansion

#7 Post by Ed Dyreen » 30 Aug 2012 13:35

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.
I agree in that,

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.

Post Reply