0

While following a tutorial for iterating over arrays in a batch script I didn't get the same result:

enter image description here

@echo off 
setlocal enabledelayedexpansion 
set topic[0] = comments 
set topic[1] = variables 
set topic[2] = Arrays 
set topic[3] = Decision making 
set topic[4] = Time and date 
set topic[5] = Operators 

for /l %%n in (0,1,5) do ( 
   echo !topic[%%n]! 
)

When I run this command I get:

enter image description here

Adrian Elder
  • 1,993
  • 3
  • 19
  • 38
  • 1
    This means that the people that wrote such a tutorial didn't tested its own code! **`:(`** I suggest you to read [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) about array management in Batch files... – Aacini Jun 05 '17 at 18:23
  • @Aacini Yes - I agree. I sent them an email. The sad part is it's a very prominent website. The whole tutorial series is like this and it's like 24 chapters. – Adrian Elder Jun 05 '17 at 18:24

1 Answers1

5

in batch spaces matter because it's an argument delimiter

set topic[0] = comments

should be

set topic[0]=comments

Something strange with cmd is that variables can terminate with a space

set topic[0] = comments
echo %topic[0] %
 comments
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36