Page 1 of 1

My program isn't working as it should.

Posted: 03 Dec 2019 21:33
by exro_dev
So I don't really know how to explain this so I will try my best. I am trying to split up one line into multiple variables but I can't seem to get the right outputs.
Here is test.bat

Code: Select all

@echo off
setlocal enableDelayedExpansion
 
:read_map
del _TEMP.txt
set map=test.bgem
set /a cell=0
 
for /F "tokens=* delims=" %%a in ('type %map%') do (
    Set /a cell+=1
    Set "output[!cell!]=%%a"
    set "output[cur]=%%a"
 
    echo !output[cur]!
    for /f "tokens=1 delims=," %%i in ("!output[%cell%]!") do set output[%cell%]_x=%%i
    for /f "tokens=1 delims=:" %%i in ("!output[%cell%]!") do set output[%cell%]_y=%%i
   
    echo !output[%cell%]_y! >> _TEMP.txt
   
    call:findString output[%cell%]_y "," pos
   
    if not defined pos echo.Substring not found&goto:eof
    echo.Position is: %pos%
    set /A pos=%pos%+1
   
   
    echo x: !output[%cell%]_x!
    echo y: !output[%cell%]_y:~%pos%,2!
    echo.
)
 
pause
 
 
 
REM https://www.dostips.com/forum/viewtopic.php?t=194 credits DosItHelp
:findString -- returns the zero based postion of one string in another string of maximum length of 1023 characters
::          -- %~1: in - varible name of a string to be serached
::          -- %~1: in - string to be found
::          -- %~3: out- return variable name, will be set to position or undefined if string not found
set "str=!%~1!"
set "str=!str:%~2=@@@@!
set "str=%str:@@@@="&REM %
if "%str%"=="!%~1!" (
   ENDLOCAL&IF "%~3" NEQ "" SET "%~3="
   GOTO:EOF
)
set str=A!str!&  rem keep the A up front to ensures we get the length and not the upper bond
                 rem it also avoids trouble in case of empty string
set len=0
set /a n=1024
set /a n^>^>=1, len+=n
 if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
 if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
 if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
 if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
 if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
 if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
 if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
 if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
 if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
 if !str:~%len%!. == . set /a len-=n
( ENDLOCAL & REM RETURN VALUES
    IF "%~3" NEQ "" SET %~3=%len%
)
GOTO:EOF

And here is test.bgem

Code: Select all

0,0:sf2/a;
12,10:sf2/a;
I don't know how to explain what I'm trying to do and I hope someone gets it but if you don't just let me know. If anyone could help me out, I would really appreciate it.

Thanks,
Exro :D

Re: My program isn't working as it should.

Posted: 04 Dec 2019 10:16
by aGerman
Something like !output[%cell%]! will not work because variable cell was updated in the body of your loop and will not expand to the current value. But first of all it would be nice to know what output you even expect for your given "test.bgem".

Steffen

Re: My program isn't working as it should.

Posted: 04 Dec 2019 20:00
by Eureka!
It looks like you created (actually: not finished) a very complex way (replacing text from a certain position to see if the "," is still there and then shifting a position to repeat the process until you find what the number between the "," and the ":" is) to get this result:

Code: Select all

output[1]_x=0
output[1]_y=0
output[2]_x=12
output[2]_y=10
That can be done with this code:

Code: Select all

@echo off
setlocal enabledelayedexpansion

set map=test.bgem
set /a cell=0


for /F "usebackq tokens=1,2 delims=,:" %%a in ("%map%") do (
	Set /a cell+=1
	set output[!cell!]_x=%%a
	set output[!cell!]_y=%%b
)


set output