
Re: Sorting tokens within a string
amel27 wrote:
Your variant doesn't approach for <LF> enters, because !STR! should expand in nested CMD, not current... But, as by default DelayedExpansion disabled, we should launch CMD.EXE twice.
I'm don't fully understand the explanation, but I agree my suggestion doesn't work. I tried lots of variations on the theme but to no avail. I'm a bit mystified why my original :sortStrTokensI works, but this suggestion doesn't.
amel27 wrote:
I think, you should try "%comspec%" (with full path) instead of short "CMD" command.
No difference in performance, unfortunately.
amel27 wrote:
Yes, "eol" very strange option, I can't understand when it works when not. Any information will be useful.
I think it is fairly straight forward, but terribly documented. See if the following examples help:
Code:
@echo off
setlocal enableDelayedExpansion
cls
set lf=^
set "testLines=1)Hello;world!lf!;2)Hello;world!lf! 3)Hello;world!lf!"4)Hello" "world""
echo !lf!testLines are:!lf!!lf!!testLines!
echo !lf!============================!lf!
set /a n=0
call :test "tokens=1,2"
call :test "tokens=1,2 eol="
call :test "tokens=1,2 eol= delims=;"
call :test "tokens=1,2 eol= "
call :test "tokens=1,2 delims=;"
call :test "tokens=1,2 eol= delims=; "
exit /b
:test
set /a n+=1
echo Call %n% options: %1
echo:
for /f %1 %%a in ("!testLines!") do (
echo a=%%a b=%%b
)
echo !lf!============================!lf!
exit /b
results:
Code:
testLines are:
1)Hello;world
;2)Hello;world
3)Hello;world
"4)Hello" "world"
============================
Call 1 options: "tokens=1,2"
a=1)Hello;world b=
a=3)Hello;world b=
a="4)Hello" b="world"
============================
Call 2 options: "tokens=1,2 eol="
a=1)Hello;world b=
a=;2)Hello;world b=
a=3)Hello;world b=
============================
Call 3 options: "tokens=1,2 eol= delims=;"
a=1)Hello b=world
a=2)Hello b=world
a="4)Hello" "world" b=
============================
Call 4 options: "tokens=1,2 eol= "
a=1)Hello;world b=
a=;2)Hello;world b=
a=3)Hello;world b=
a="4)Hello" b="world"
============================
Call 5 options: "tokens=1,2 delims=;"
a=1)Hello b=world
a=2)Hello b=world
a= 3)Hello b=world
a="4)Hello" "world" b=
============================
Call 6 options: "tokens=1,2 eol= delims=; "
a=1)Hello b=world
a=2)Hello b=world
a=3)Hello b=world
a="4)Hello" b="world"
============================
Notes:
Call 1: default eol=<semicolon>, default delims=<space><tab>
Line 2) starting with <semicolon> stripped
Call 2: eol=<quote>, default delims=<space><tab>
Very interesting that the <quote> both sets the eol option and terminates the argument string!
Line 4) starting with <quote> stripped
Call 3: eol=<space>, delims=<semicolon>
Line 3) starting with <space> stripped
Call 4: eol=<space>, default delims=<space><tab>
eol matces one of the delimiters, so it is disabled and all lines preserved!
Call 5: default eol=<semicolon>, delims=<semicolon>
eol matces one of the delimiters, so it is disabled and all lines preserved!
Call 6: eol=<space>, delims=<semicolon><space>
eol matces one of the delimiters, so it is disabled and all lines preserved!
I was not able to figure out how to specify <quote> as a delimiter, but I don't think I really care either.
Dave Benham