MrKnowItAllxx wrote:Call me lazy, but I didn't want to read 2 pages of comments, so I'm posting this solution w/o knowing if it has been suggested already
I will let Dave be the Judge of that.
Moderator: DosItHelp
MrKnowItAllxx wrote:Call me lazy, but I didn't want to read 2 pages of comments, so I'm posting this solution w/o knowing if it has been suggested already
dbenham wrote:@jeb - interesting non looping algorithm. But you assume the string does not contain <LF>
dbenham wrote:I believe I have a bullet-proof variation that avoids using any awkward characters like <LF> and supports all possible string values. Though I'm not convinced I've done enough testing.
...
The code above requires 7 search and replaces, the same as the modified Squashman technique. So I don't think it has any advantage over it, especially if you flatten out the Squashman technique.
Code: Select all
@echo off
set "str=,,,!Hello Goodbye!,,This&That,value2 opt,,,value3,,value4 opt,,,value5,,,,,"
setlocal enableDelayedExpansion
if defined str (
rem Suround each comma "," -> "a,b"
echo #%%~L#
set ^"str=!str:,=a,,!"
rem replace ",a,"->"" (empty)
set ^"str=!str:,a,=!"
rem replace "a,,"->","
set ^"str=!str:a,,=,!"
)
set str
timbertuck wrote:i do have a question for jeb, could you explain your code to me, i can understand some of it, but the rest i am lost. i get the LF portion, but the set strings i don't get. thanks!
Code: Select all
@echo off
set "str=,,,!Hello Goodbye!,,This&That,value2 opt,,,value3,,value4 opt,,,value5,,,,,"
set str
setlocal enableDelayedExpansion
::Comma collapsing begins here
::Limited to at most 2730 commas in a string
if defined str (
set ^"str=!str:,=a,,!"
set ^"str=!str:,a,=!"
set ^"str=!str:a,,=,!"
if "!str:~0,1!"=="," set "str=!str:~1!"
if defined str if "!str:~-1!"=="," set "str=!str:~0,-1!"
)
::comma collapsing ends here
set str
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "str=,,,!Hello Goodbye!,,This&That,value2 opt,,,value3,,value4 opt,,,value5,,,,,"
rem Replace question-mark by Ascii-223
set "str=!str:?=▀!"
rem Replace standard delimiters: space by Ascii-192 and semicolon by Ascii-219:
set "str=!str: =└!"
set "str=!str:;=█!"
rem Collect string tokens and separate they with one comma
set result=
for %%a in (!str!) do set "result=!result!%%a,"
rem Replace back original characters
set "result=!result:▀=?!"
set "result=!result:└= !"
set "result=!result:█=;!"
rem The result have one additional comma at end
echo !result:~0,-1!
Aacini wrote:PS - Did you realize that original topic requested to change semicolons, not commas