You are not logged in.

#1 08 Nov 2014 08:07

TJW
New Member
Registered: 08 Nov 2014
Posts: 3

list of vars

Hi,
I am working on a .cmd file
I have a list of variables that consist of a text and number, like: a1,a2,a3,a4,a5,a6...a100 (pulled from a csv)
I need to use all of these in a row and do the same thing with all of them. Im trying to make a loop.

SET a1=100
SET a2=200
SET a3=300
SET a100=10000

SET n=0
:loop
SET /a n=n+1
SET aX=a%n%
echo %%aX%% (Which should be %a1%...%a2%...%a3%... )
goto loop

But it doesn't work sad
Is there a way to do this?
Hopefully you can help me.

My variables are more complex than just numbers and I need to do more than just echo, but this should explain the problem.

Thanks!

Offline

#2 08 Nov 2014 09:29

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: list of vars

You can either use

call echo %%aX%%

or

setlocal enabledelayedexpansion
echo !aX!

Last edited by Shadow Thief (08 Nov 2014 09:29)

Offline

#3 08 Nov 2014 09:43

TJW
New Member
Registered: 08 Nov 2014
Posts: 3

Re: list of vars

Thank you for your quick response!
In both cases you suggest it returns "a1" , "a2", "a3" and not "100","200", "300"
What am I doing wrong?

Thanks

Offline

#4 08 Nov 2014 09:50

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: list of vars

Oh, wait, I misunderstood what you wanted. Try

setlocal enabledelayedexpansion
echo !a%n%!

and just forget about the aX thing altogether.

Offline

#5 08 Nov 2014 10:02

TJW
New Member
Registered: 08 Nov 2014
Posts: 3

Re: list of vars

Great! big_smile
Thanks

Offline

#6 11 Nov 2014 22:41

Aacini
Member
Registered: 05 Dec 2012
Posts: 149

Re: list of vars

I want to note a couple points about your question.

First of all, I suggest you to adopt the standard way to write array elements in Batch files enclosing the indices in square brackets this way: SET a[1]=100 instead of: SET a1=100. This method have some advantages that are described at this post but the main reason is to write clearer code, so index variables are clearly identified and not mistaken as any other thing. The details on array management in Batch files are fully described here. The following are general recommendations about how to use arrays in a program.

The variables you shown at beginning of your question may be created this way:

for /L %%i in (1,1,100) do set a[%%i]=%%i00

To create an array with a few, different elements, you may use this:

set n=0
for %%a in (123 456 789 234 345 456 567 678 789 890 345) do (
   set /A n+=1
   set a[!n!]=%%a
)

If you want to join previous values in a line (separating each one by a space), you may use this:

set "line="
for /L %%i in (1,1,%n%) do set "line=!line! !a[%%i]!"
echo %line%

Previous segment will show the values of a[1]..a[%n%] elements in the same line preceded by a space. If you want to eliminate the space, just use echo %line:~1% instead.

If you need to repeat previous segment many times with many data, there is an interesting trick to speed up such process, but I don't show it this time because it is somewhat advanced (and confusing).

I hope it helps...

Antonio

Last edited by Aacini (11 Nov 2014 22:47)

Offline

Board footer

Powered by