jeb wrote: ↑19 Feb 2018 16:33
Hi Aacini,
really nice
It takes a minute to understand your code
Once upon a time, someone told me, that it's nice to explain a bit more and ever after I tried hard.
I suppose for some readers it would be helpful, when you show your idea.
jeb
IcarusLives wrote:
I'm really interested in how this works, but I'm struggling to understand it. Can you please explain with some detail this method?
The purpose of the method is to split
two strings in their parts. This can be easily done
in two lines and then combine the parts in a third line:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "str=1.0.2.25"
set "vars=Major Minor Revision Subrev"
set "i=1" & set "s!i!=%str:.=" & set /A i+=1 & set "s!i!=%"
set "i=1" & set "v!i!=%vars: =" & set /A i+=1 & set "v!i!=%"
for /L %%i in (1,1,%i%) do set "!v%%i!=!s%%i!"
echo Major: %Major%, Minor: %Minor%, Revision: %Revision%, Subrev: %Subrev%
In this way, the goal is to complete the same split
in just one line. To do that, in each division of the first string we must "split" the second string, that is, perform the equivalent process: take the value before the first dot to assign it to the variable,
and ignore the rest of values. A first attempt to ignore the rest of the values (after the first dot) is inserting a "REM" command in the usual way:
Code: Select all
set "p=%%"
set "v=%vars: =" & call set "!v!=!p!str:.=& rem !p!" & set "str=!str:*.=!" & set "v=%" & set "!v!=!s!"
^^^^ \________________/
The purpose of the marked part is to insert a REM command in place of the dots in the value of STR variable. However, this method have two problems: it is necessary to escape several special characters in this part in order to correctly execute the REM command, otherwise it is just assigned to the !V! variable. Anyway, when the REM command is successfully executed, it causes to ignore
all commands after it, including the rest of commands that should split the rest of parts in the first string.
A workaround to do the same process without using a REM command is simple: first, take the rest of values after the first dot:
set "s=!str:*.=!"; then, eliminate such a part from the variable (this gives just the first part):
call set "!v!=!p!str:.!s!=!p!". Finally, assign the rest of values to the same variable (in preparation for the next part):
set "str=!s!".
Code: Select all
set "v=%vars: =" & set "s=!str:*.=!" & call set "!v!=!p!str:.!s!=!p!" & set "str=!s!" & set "v=%" & set "!v!=!s!"
Note that three previous steps are performed
in each part of the split of first variable, that is, they are placed inside the only percent-signs in the line. At end, the last values remains in their respective variables but have not been processed, so an additional
set "!v!=!s!" is inserted at end of line...
As jeb would say: it's obvious!
Antonio