0

How do i split a string dynamically inside LOOP

I Want split a string dynamically inside LOOP,But the code below not working perfectly , it look simple but it really confusing me what actually make my coding not working perfectly, maybe another methods can it be done ?

*Extra question,How to get string lenght in single loop.

sample.bat:

@echo off & setlocal EnableDelayedExpansion

:: Configuration
set "Num_1= 123"
set "Num_2= 45"
set "Num_3= 6789"
::set "Num_4= 4343321345677"
::set "Num_5= 98987675567543213"
::set "Num_6= 6766578987765544339"
::set "char1= anf,ki"
::set "char2= aewr(677)ni"


:: Get length 
:length 
if not "!Num_1:~%len%!"=="" set /A len+=1 & goto length 
set /A L_Num_1=%len% - 1 
set len=0

:length2
if not "!Num_2:~%len%!"=="" set /A len+=1 & goto length2
set /A L_Num_2=%len% - 1 
set len=0

:length3
if not "!Num_3:~%len%!"=="" set /A len+=1 & goto length3
set /A L_Num_3=%len% - 1 


Set Count="

:: Split
set "cha=1"
for /l %%n in (1,1,3) do (
       Set Count="
               for /l %%o in (!L_Num_%cha%!,-1,0) do (
              Set /a "cha=cha + 1 | 1 "
              Set /a "Count+=1"
              set "SNum(%%n)_!Count!=!Num_%%n:~%%o,1!"
         )
 )
set SNum
pause

Current output

SNum(1)_7=3
SNum(1)_8=2
SNum(1)_9=1
SNum(1)_10= 

SNum(2)_2=5
SNum(2)_3=4
SNum(2)_4= 

SNum(3)_1=8
SNum(3)_2=7
SNum(3)_3=6 
SNum(3)_4=

Output what i need

(from Variable Num_1 )

SNum(1)_1=3
SNum(1)_2=2
SNum(1)_3=1

(from Variable Num_2 )

SNum(2)_1=5
SNum(2)_2=4

(from Variable Num_3 )

SNum(3)_1=9
SNum(3)_2=8
SNum(3)_3=7
SNum(3)_4=6

Split each variable/string from backwards and create new array start from 1. i know it can be done by manually split string from variable one by one ,but at my situation i have split more dynamically because not only three string from variable i have to split,sometimes it have more than 10 variable.

5
  • 2
    I would advise you not use parentheses in your variable names. Use [ ] instead. Commented Apr 20, 2018 at 13:32
  • Why do you have a leading space assigned to all your variables? Commented Apr 20, 2018 at 14:12
  • Do you mean space after "Snum "? Actually from original output it doestt have any space ,after i paste the output here ,i dont know why space aoutomatically inserted to my writing Commented Apr 20, 2018 at 14:28
  • Editing your question tells me you completely misunderstood my question. You were getting output variables with a space because your input variables have a space. So the spaces were not magically put into your code or output before you copied and pasted it to your question. Commented Apr 20, 2018 at 15:22
  • REALY SOORY, now i 100% understand what your question, leading space at input variable i really does't need it,its just happen because i multiple time modified this code and trying new ways to make this code work perpectly... Commented Apr 20, 2018 at 15:46

1 Answer 1

1

The method used to get the string length is to test the character at the middle of the maximum length and successively cut this maximum length at half. The net effect is like collect the individual bits (with values in base 2: 1, 2, 4, 8, etc) of the real string length.

@echo off
setlocal EnableDelayedExpansion

:: Configuration
set "Num_1=123"
set "Num_2=45"
set "Num_3=6789"

rem Set number of variables to process
set "vars=3"

rem Get length of "Num_1".."Num_%vars%" variables into "L_Num_1".."L_Num_%vars%"
for /L %%n in (1,1,%vars%) do (
   set "L_Num_%%n=0"
   for /L %%a in (12,-1,0) do (
      set /A "newLen=L_Num_%%n+(1<<%%a)"
      for %%b in (!newLen!) do if "!Num_%%n:~%%b,1!" neq "" set "L_Num_%%n=%%b"
   )
)

rem Split %vars% variables into individual characters
for /L %%n in (1,1,%vars%) do (
   set "cha=0"
   for /L %%o in (!L_Num_%%n!,-1,0) do (
      set /A "cha=cha+1"
      set "SNum[%%n]_!cha!=!Num_%%n:~%%o,1!"
   )
)
set SNum

Note that the first tested position is 2^12 that is 4096, the half of the maximum string length. This is achieved via a left shift bitwise operation: 1<<%%a.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.