0

How do I set my if statement to call on different predefined variables based on user input.

Example 1 is red 2 is orange 3 is blue 4 is random. If user puts 1 they get red. If they put 2 they get orange. If they put 3 they get blue. If they put 4 they get either red, orange, or blue.

  • What have you already tried, and what specifically are you not able to do? Have you tried typing `IF /?` into your command prompt for an explanation of the IF command? – unclemeat Apr 16 '15 at 00:27
  • If %example% == 4 goto colorSelect Then after that I set colorSelect so. ... :colorSelect Random (red, orange, blue) pause exit – Zachary Nelson Apr 16 '15 at 00:43
  • `if %example% == 4 set /a example=%random% % 3 + 1` Then do your processing of example – Scott C Apr 16 '15 at 00:57

3 Answers3

0

The following script shows one way of doing it, using arrays of colors and a method for doing double expansion on a variable (allowing for array access):

@setlocal enableextensions enabledelayedexpansion
@echo off

set color[1]=red
set color[2]=orange
set color[3]=blue

:loop
    set /p inp="Enter a number [1=red, 2=orange, 3=blue, 4=random]: "
    if "%inp%"=="4" set /a "inp = %RANDOM% %% 3 + 1"
    call set color=%%color[%inp%]%%
    if "%color%"=="" goto loop

endlocal && set color=%color%

For a more generalised solution, you can look at the following script, which better handles the prompting for arbitrary colors:

@setlocal enableextensions enabledelayedexpansion
@echo off

rem Clear out all color variables then create array.

set color=junk
for /f "delims==" %%a in ('set color') do set %%a=

set /a "count = 0"
set /a "count = count + 1" && set color[!count!]=red
set /a "count = count + 1" && set color[!count!]=orange
set /a "count = count + 1" && set color[!count!]=blue
set /a "count = count + 1" && set color[!count!]=green
set /a "next = count + 1"

rem Loop until color is valid.

:loop
    echo.Choices:
    for /l %%a in (1,1,%count%) do (
        set value=!color[%%a]!
        echo.   %%a. !value!
    )
    echo.   %next%. Random choice from above

    set /p inp="Enter a number: "
    rem set inp=1

    rem Special handling, choose random value

    if "%inp%"=="%next%" set /a "inp = %RANDOM% %% count + 1"

    call set color=%%color[%inp%]%%
    if "%color%"=="" goto loop

rem Exit local scope, "leaking" color value.

endlocal && set color=%color%
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 2
    Your `doubleset` "nifty trick" is not required at all; you may directly use: `set color=!color[%inp%]!`, or `call set color=%%color[%inp%]%%`. See: http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990 – Aacini Apr 16 '15 at 02:46
  • @Aacini, good call, I've incorporated that and added a more flexible option as well. – paxdiablo Apr 16 '15 at 03:31
  • There are a couple points in your new method that may be achieved in a simpler way. See [my solution](http://stackoverflow.com/questions/29663158/how-to-set-batch-if-statement-to-select-random-predefined-variables/29679117#29679117) – Aacini Apr 16 '15 at 15:25
0
@ECHO Off
SETLOCAL
SET "choices=1=red 2=blue 3=green 4=random"
SET /p inp="[%choices%]: "
FOR /f %%a IN ('echo %choices: =^&echo %') DO SET /a maxchoice=%%a
IF "%inp%"=="%maxchoice%" SET /a inp=%RANDOM% %% (maxchoice - 1) +1
FOR /f "tokens=1,2" %%a IN ('echo %choices: =^&echo %') DO IF "%%a"=="%inp%" SET "hue=%%b"
ECHO %hue%
GOTO :EOF

Here's my version. All you need to do is follow the bouncing ball setting up choices and ensure that random is the last selection.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

I started to write a comment as reply to new paxdiablo's solution, but it becomes too large, so I prefer to write my own solution here with all those points included:

@echo off
setlocal EnableDelayedExpansion

rem Create the color array
set n=0
for %%a in (red orange blue green) do (
   set /A n+=1
   set color[!n!]=%%a
)
set /A next=n+1

rem Show the available colors menu
echo Choices:
echo/
for /L %%i in (1,1,%n%) do echo    %%i. !color[%%i]!
echo    %next%. Random choice from above
echo/

rem Loop until color is valid
:loop
   set /P "inp=Enter a number: "
   if "%inp%" equ "%next%" set /A inp=%random% %% n + 1
   set color=!color[%inp%]!
if "%color%" equ "" goto loop

echo/
echo Color: %color%
Aacini
  • 65,180
  • 12
  • 72
  • 108