Setting numerically labeled vars to strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dankbatcher
Posts: 1
Joined: 10 Feb 2016 08:54

Setting numerically labeled vars to strings

#1 Post by Dankbatcher » 10 Feb 2016 09:11

So basically what I have is a random number generator.

Code: Select all

set /a rngn=%random% %% 25


Then a big loop that will set a variable depending on how many times it has cycled through the loop.
The slot variable has already been declared, and equals 1.

Code: Select all

:setvars
set /a rngn=%random% %% 25
echo %rngn%
IF /i %rngn%==0 (
set itm%slot%=stick
)
IF /i %rngn%==1 (
set itm%slot%=log
)
IF /i %rngn%==2 (
set itm%slot%=rock
)
IF /i %rngn%==3 (
set itm%slot%=flint
)
IF /i %rngn%==4 (
set itm%slot%=steel_fragment
)
IF /i %rngn%==5 (
set itm%slot%=rope_fragment
)
IF /i %rngn%==6 (
set itm%slot%=axe
)
IF /i %rngn%==7 (
set itm%slot%=log
)
IF /i %rngn%==8 (
set itm%slot%=cloth_fragment
)
IF /i %rngn%==9 (
set itm%slot%=tunic
)
IF /i %rngn%==10 (
set itm%slot%=pants
)
IF /i %rngn% GEQ 10 (
goto af
)
set /a slot= %slot% +1
:af
echo !itm%slot%!
set /a timer= %timer% +1
IF %timer% GEQ 10 (
goto check
) ELSE (
goto setvars
)
:check


I didn't use a for loop becuase really it wouldn't have been much shorter. I do know that the loop works though, and it isn't an issue.

Basically it's a random item generator, which generates a number, which is recognized as an item, which is represented by a string. Any number it generates that isn't LEQ 10 is not considered an item, and those values are only generated for a chance of not getting an item.

Now when i try to

Code: Select all

echo !itm%slot%!
(Note that delayed expansion is turned on) It either returns 0 or

Code: Select all

ECHO is on.

It should be returning a string that is associated with the random number assigned to that variable.
Is this a problem with my

Code: Select all

set itm%slot%=string
or my

Code: Select all

echo !itm%slot%!
I feel like I'm making this more complex than it should be. (Well, I already made it like that with my silly loop)

Feel free to point out any more errors with my code, but I'd prefer to not have to rewrite that whole loop.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Setting numerically labeled vars to strings

#2 Post by foxidrive » 10 Feb 2016 09:31

Dankbatcher wrote:Is this a problem with my

Code: Select all

set itm%slot%=string
or my

Code: Select all

echo !itm%slot%!
I feel like I'm making this more complex than it should be.


In the spirit of debugging: to test the question you have asked above, you can script it in a simple way and it will tell you the answer:

Code: Select all

@echo off
setlocal enabledelayedexpansion

set slot=2

set itm%slot%=string

echo !itm%slot%!
pause


The echo command returns that error message when there is no text after the echo command - or an empty variable.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Setting numerically labeled vars to strings

#3 Post by thefeduke » 10 Feb 2016 19:50

This is not a solution, but will avoid the empty variable problem. If you change your echo command to:

Code: Select all

echo itm%slot%=!itm%slot%!
you should learn something about what is happening.

John A.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Setting numerically labeled vars to strings

#4 Post by Aacini » 10 Feb 2016 21:33

A variable that have a base name plus a changing subscript is known as an array; these variables have the advantage that it is not necessary to process each array element individually, but just write once a general form that represents all elements, and then select the appropriate one via the value of a subscript. A small example may aid to visualize this concept in an easier way.

Suppose that you have a variable named "base" and that you want add it a second variable, named "element", that you know can only have the values 10, 12 and 23. What method would you use to add "element" to "base"? This one:

Code: Select all

IF /i %element%==10 (
   set /A base=base+10
)
IF /i %element%==12 (
   set /A base=base+12
)
IF /i %element%==23 (
   set /A base=base+23
)

... or this one:

Code: Select all

set /A base=base+element

I want not to elaborate on this example; I just want that you note the similarities of this example with your code.

This is the way I would perform this process using an array:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Initialization step: define the elements of "item" array
set i=0
for %%a in (stick log rock flint steel_fragment rope_fragment axe log cloth_fragment tunic pants) do (
   set item[!i!]=%%a
   set /A i+=1
)


rem Populate "itm" array with elements from "item" array in random order

set slot=1
:setvars
   set /A rngn=%random% %% 25
   echo %rngn%
   if %rngn% leq 10 (
      set itm%slot%=!item[%rngn%]!
      echo !itm%slot%!
      set /a slot= %slot% +1
   )
IF %slot% leq 10 goto setvars

:check

Output example:

Code: Select all

22
2
rock
1
log
0
stick
15
22
14
5
rope_fragment
17
18
10
pants
8
cloth_fragment
13
21
13
20
22
3
flint
15
5
rope_fragment
10
pants
0
stick

Antonio

Post Reply