1

I'm trying to efficiently name variables a1 through a9 with !%f%! through !%q%!. I tried creating a for loop to complete this:

set w=0
for %%K in (f g h i k l m o q) do (
    set /a w+=1
    set /a b%w%=%!%%K!%
    set /a a%w%=!b%w%!%%13
)

The error is that there is a missing operator, so I'm assuming that the parenthesis and exclamation marks are being misread. How would I go about fixing this problem?

TL;DR

Make:

set /a a1=!%f%!%%13
...
set /a a9=!%q%!%%13

...more efficient.

To clarify, %f% is equal to say 4D and %4D% is equal to 9.

I need the modulus of 9 and 13 as the a1.

14
  • Won't fix it, but instead of doing set /a w=w+1 - you should do set /a w+=1. Commented Jan 19, 2014 at 22:47
  • Is that like the x++ incrementer for batch? Commented Jan 19, 2014 at 22:51
  • Pretty much. What is the point of setting your b1 through b9 variables? Commented Jan 19, 2014 at 22:52
  • I was just trying to eliminate a pair of percent signs in an attempt to fix the problem, and does set /a w-=1 decrement w? Commented Jan 19, 2014 at 22:54
  • Also, setting w to 0 is not necessary at the start. setting an empty variable using /a and +=1 will set the variable to 1. Commented Jan 19, 2014 at 22:56

2 Answers 2

2

I believe at least one problem you are facing, based on your comment below your question, is that a variable name of 4D will be interpreted by SET /A as the number 4 followed by variable D, which would be invalid.

Another issue you have is that the expansion of %w% will be a constant value within the loop. You need delayed expansion !w! instead.

I believe this is what you want.

set w=0
for %%K in (f g h i k l m o q) do (
  set /a w+=1
  for %%V in (!%%K!) do set /a a!w!=!%%V!%%13
)

If none of your variable names begin with a number, then you could simply use

set w=0
for %%K in (f g h i k l m o q) do (
  set /a w+=1
  set /a a!w!=!%%K!%%13
)

I try to avoid using a number as the leading character for a variable name for two reasons:

1) SET /A will not expand the variable properly, as you are experiencing in your question.

2) Normal expansion of %4D% would be interpreted as value of argument %4, followed by D, followed by a % that would be stripped from the result.

Sign up to request clarification or add additional context in comments.

3 Comments

So is this generally how I can fix my problem with variables starting with numbers in any case? Because right now I am naming the variables a2D, a4C, etc. and then making them substrings.
And I'm slightly unclear on the usage of !%%K! instead of just %%K in the for %%V loop
See my updated answer as to why a variable name starting with a number is to be avoided. The value of %%K is f. I use !%%K! to get the string 4D into %%V so that I can then use !%%V! which equates to !4D!.
0
setLocal enableDelayedExpansion
set w=0
for %%K in (f g h i k l m o q) do (
    set /a w+=1
    set a!w!=^^!%%%%K%%^^!%%%%13
)

:: as a test
echo !a5!

This should do the trick for you. Carat ^ is the batch escape character.

5 Comments

That cannot possibly work - the expansion of %w% will remain constant within the loop. Delayed expansion is needed. Also, I don't know what you are attempting with the carets - you cannot escape a % with a caret.
Yeah, realized my various obvious problems after testing it.
The output that I am getting is: !%f%!%%13
I need the actual result of the operation
So if %f% expands to foo and !foo! expands to bar you're expecting the output to be bar%%13?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.