Reputation: 115
I'm trying to select 1 of 10 random strings from a predetermined array using the code bellow. When %Answer% is echo it prints "randomNumber".
Any ideas where I'm going wrong.
echo off
setlocal enabledelayedexpansion
set /a randomNumber=%RANDOM% %%10
set Answer=!foo[(%randomNumber%)]!
echo %Answer%
pause
EDIT: here is how I create the array (context: I'm trying to code a Magic 8 Ball)
set a/ foo[0]=Majic8BallDon'tKnow.Majic8BallSaysAskAgain..
set a/ foo[1]=You are not worthy of an answer..
set a/ foo[2]=You wouldn't understand if I told you..
and so on to foo[9]
Upvotes: 1
Views: 109
Reputation: 14290
You have two choices.
Use delayed expansion.
setlocal enabledelayedexpansion
set /a randomNumber=%RANDOM% %%10
set Answer=!foo[(%randomNumber%)]!
echo %Answer%
pause
Or use the call command
set /a randomNumber=%RANDOM% %%10
call set Answer=%%foo[(%randomNumber%)]%%
echo %Answer%
pause
I don't know how you are creating your array, so this answer may need some tweaking.
Upvotes: 2