-1

My batch file contains:

@echo off

:default_grid
set "0=-"
set "1=-"
set "2=-"
set "3=-"
set "4=-"
set "10=-"
set "11=-"
set "12=-"
set "13=-"
set "14=-"
set "20=-"
set "21=-"
set "22=-"
set "23=-"
set "24=-"
set "30=-"
set "31=-" 
set "32=-"
set "33=-"
set "34=-"
set "40=-"
set "41=-"
set "42=-"
set "43=-"
set "44=-"

set pos=22
if %pos% EQU 22 set "22=O"

:grid

echo.
echo "%0%" "%10%" "%20%" "%30%" "%40%"
echo "%1%" "%11%" "%21%" "%31%" "%41%"
echo "%2%" "%12%" "%22%" "%32%" "%42%"
echo "%3%" "%13%" "%23%" "%33%" "%43%"
echo "%4%" "%14%" "%24%" "%34%" "%44%"
echo.
pause >nul

I am having a hard time understanding what I am doing wrong.
I have 25 variables that should potentially display -.

What I want to see is:

- - - - -
- - - - -
- - - - -
- - - - -
- - - - -

But instead I see:

"C:\Users\MuggyYak\Desktop\5x5.bat"10203040"
"11213141"
"12223242"
"13233343"
"14243444"

Does anybody know what to do?

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 6
    Never use variables whose names begin with decimal digits, because `%0`, `%1`, `%2`, `%3`, etc. are interpreted as [command line arguments](http://ss64.com/nt/syntax-args.html)! – aschipfl Sep 10 '18 at 17:26
  • Thanks so much, @aschpfl I try that out. – Joshua Chubbs Sep 10 '18 at 17:32
  • 1
    Open a command prompt window and run `call /?` and you know why it is an awful idea to name environment variables starting with a digit. – Mofi Sep 10 '18 at 17:40

1 Answers1

0

Although you should not use variables with names that start with digit as suggested before, you may make your code work if you don't expand the variables via percent, but via exclamation mark and Delayed Expansion:

@echo off
setlocal EnableDelayedExpansion

:default_grid
set "0=-"
set "1=-"
set "2=-"
set "3=-"
set "4=-"
set "10=-"
set "11=-"
set "12=-"
set "13=-"
set "14=-"
set "20=-"
set "21=-"
set "22=-"
set "23=-"
set "24=-"
set "30=-"
set "31=-" 
set "32=-"
set "33=-"
set "34=-"
set "40=-"
set "41=-"
set "42=-"
set "43=-"
set "44=-"

set pos=22
if %pos% EQU 22 set "22=O"

:grid

echo.
echo "!0!" "!10!" "!20!" "!30!" "!40!"
echo "!1!" "!11!" "!21!" "!31!" "!41!"
echo "!2!" "!12!" "!22!" "!32!" "!42!"
echo "!3!" "!13!" "!23!" "!33!" "!43!"
echo "!4!" "!14!" "!24!" "!34!" "!44!"
echo.
pause >nul

However, the output of this program is this:

"-" "-" "-" "-" "-"
"-" "-" "-" "-" "-"
"-" "-" "O" "-" "-"
"-" "-" "-" "-" "-"
"-" "-" "-" "-" "-"

That is very different from the output you show above...

So, if you want to show the values "without the quotes of course", why you included the quotes? :\

Aacini
  • 65,180
  • 12
  • 72
  • 108