0

I had a code piece to do loop from an array, but end up with goto was unexpected at this time. What is wrong here?

@echo off 
set len = 3 
set obj[0].Name = Joe 
set obj[0].ID = 1 
set obj[1].Name = Mark 
set obj[1].ID = 2 
set obj[2].Name = Mohan 
set obj[2].ID = 3 
set i = 0 
:loop 

if %i% equ %len% goto :eof 
set cur.Name= 
set cur.ID=

for /f "usebackq delims==.tokens=1-3" %%j in (`set obj[%i%]`) do ( 
   set cur.%%k=%%l 
) 
echo Name = %cur.Name% 
echo Value = %cur.ID% 
set /a i = %i%+1 
goto loop

After remove these space as below:

@echo off 
set len=3 
set obj[0].Name=Joe 
set obj[0].ID=1 
set obj[1].Name=Mark 
set obj[1].ID=2 
set obj[2].Name=Mohan 
set obj[2].ID=3 
set i=0 
:loop 

if %i% equ %len% goto :eof 
set cur.Name=
set cur.ID=

for /f "usebackq delims==.tokens=1-3" %%j in (`set obj[%i%]`) do ( 
   set cur.%%k=%%l 
) 
echo Name=%cur.Name% 
echo Value=%cur.ID% 
set /a i=%i%+1 
goto loop

I got these result

  • Name=
  • Value=
  • Name=
  • Value=
  • Name=
  • Value=

???

After used obj[%i%], the below code:

@echo off 
set len=3
set obj[0].Name=Joe
set obj[0].ID=1
set obj[1].Name=Mark
set obj[1].ID=2
set obj[2].Name=Mohan
set obj[2].ID=3
set i=0
:loop 

if %i% equ %len% goto :eof 
set cur.Name=%obj[%i%].Name%
set cur.ID=%obj[%i%].ID%

for /f "usebackq delims==.tokens=1-3" %%j in (`set obj[%i%]`) do ( 
   set cur.%%k=%%l 
) 
echo Name=%cur.Name% 
echo Value=%cur.ID% 
set /a i=%i%+1 
goto loop

Got the below results

  • Name=i
  • Value=i
  • Name=i
  • Value=i
  • Name=i
  • Value=i
goodywp
  • 21
  • 6
  • 1
    why are there spaces before/after your `=` in set? should be `set "obj[0].Name=Joe"` Also, you set `obj[%i%]` but never use it? – Gerhard Feb 06 '19 at 14:06
  • Related: [SET variable not being read in .BAT file](https://stackoverflow.com/q/41710106). – aschipfl Feb 06 '19 at 14:55
  • 2
    You are missing a space between the delimiters and tokens. My best practice is always to put the `DELIMS` last. `"usebackq tokens=1-3 delims==."` – Squashman Feb 06 '19 at 17:08

2 Answers2

0

I have removed spaces around equal sign of "set":

@echo off 
set /a len=3 

set obj[0].Name=Joe 
set obj[0].ID=1 
set obj[1].Name=Mark 
set obj[1].ID=2 
set obj[2].Name=Mohan 
set obj[2].ID=3 

set /a i=0 

:loop 
    if "%i%" equ "%len%" goto :eof 
    set cur.Name=
    set cur.ID=

    for /f "usebackq delims==. tokens=1-3" %%j in (`set obj[%i%]`) do ( 
       set cur.%%k=%%l 
    ) 
    echo Name=%cur.Name% 
    echo Value=%cur.ID% 
    set /a i=%i%+1 
goto loop
:eof

And the output looks like:

Name=Joe
Value=1
Name=Mark
Value=2
Name=Mohan
Value=3
mihai_mandis
  • 1,578
  • 1
  • 10
  • 13
0

Personally, I would suggest the following much simpler code:

@echo off 
setlocal EnableDelayedExpansion

set "obj[0].Name=Joe"
set "obj[0].ID=1"
set "obj[1].Name=Mark"
set "obj[1].ID=2"
set "obj[2].Name=Mohan"
set "obj[2].ID=3"

:loop
for /L %%A IN (0 1 2) do (
    for /F "delims=. tokens=2" %%B in ('set obj[%%A] ^| sort /R') do (
        set "output=%%B"
        echo !output:ID=Value!
    )
)

So that the output will be:

Name=Joe
Value=1
Name=Mark
Value=2
Name=Mohan
Value=3
double-beep
  • 5,031
  • 17
  • 33
  • 41