String Manipulation examples...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: String Manipulation examples...

#16 Post by dbenham » 16 Jun 2011 15:43

jeb wrote:
dbenham wrote:If you know of a test case where ECHO: fails, I'd be interested in seeing it. I think I saw a claim that there was a case, but I haven't seen the actual code.

Here is a sample "echo" VS "echo." VS "echo:"
both echo. and echo: fail

Thanks jeb. I finally got it. I'd seen that post before, as well as ECHO. FAILS to give text or blank line. I'm amazed how I can read a post and completely miss important points one day, and see it later and everything seems plain as day. Better late than never :!:

I am finally a convert to ECHO(

Dave Benham

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: String Manipulation examples...

#17 Post by jeb » 17 Jun 2011 00:10

dbenham wrote:I am finally a convert to ECHO(

I'm always happy to convince someone. :)

But I can't understand, why you try to expand the echo command in a macro with delayed expansion :?:

And if you can change it a bit this could work, too.

Code: Select all

setlocal EnableDelayedExpansion
set cmd=echo
!cmd!(Hello


jeb

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: String Manipulation examples...

#18 Post by jeb » 17 Jun 2011 03:08

I changed it a bit, so it should work too, and it is safe against the content.

Code: Select all

setlocal EnableDelayedExpansion
set cmd=echo
set "var1=/../mybat.bat"
set "var2=/?"
set "empty="
set "(="
!cmd!!(!!var1!
!cmd!!(!!var2!
!cmd!!(!!empty!
!cmd!!(!
echo ENDE


jeb

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: String Manipulation examples...

#19 Post by dbenham » 17 Jun 2011 06:08

jeb wrote:But I can't understand, why you try to expand the echo command in a macro with delayed expansion :?:

On rare occasions I find myself wanting to dynamically build and execute code within a loop. Typically this is when I am trying to test/compare multiple options, but I vaguely remember one other time in functional code. I realize since this is only an ECHO with no effect on the environment, I could simply CALL out of the loop and SETLOCAL DISABLEDELAYEDEXPANSION before %cmd% and ENDLOCAL after it. But I was looking for a case where ECHO( failed, and delayed expansion is the first logical thought within a loop. BTW - since this is off topic from the original post, I had posted some additional observations/question on ECHO( issue at ECHO. FAILS to give text or blank line. Sorry, I should have given notice of the jump.

jeb wrote:I changed it a bit, so it should work too, and it is safe against the content.
( referring to !cmd!!(! )

Very clever. I like it :!: :D
I tested and !cmd!!(!!var1:~0,4! works as well

Dave Benham

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: String Manipulation examples...

#20 Post by Acy Forsythe » 17 Jun 2011 15:50

Orange,

I read through the thread you posted and I get the %% signs or at least I thought I did...

This part is messing me up:

call call echo:%%%%a_%%a_%a_cup%%%%%%%

I removed the a_'s and left the front and back % signs, and did not get the expected result...

I'm hoping that figuring it out will help me figure this out:

Code: Select all

:: I want to fix this line because it doesn't work:

set line=%%line%%space%:~0,80%



I personally feel like cmd.exe is just being stubborn and pig headed by not parsing that line the way I want it to. The result I was expecting is for it to act like this:

Code: Select all

set line=%line%%space%
set line=%line:~0,80%

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: String Manipulation examples...

#21 Post by Ed Dyreen » 17 Jun 2011 15:55

Code: Select all

:: I want to fix this line because it doesn't work:

set line=%%line%%space%:~0,80%

Code: Select all

call set "line=%%line%%space:~0,80%%"

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: String Manipulation examples...

#22 Post by dbenham » 17 Jun 2011 16:52

Acy Forsythe wrote:This part is messing me up:

call call echo:%%%%a_%%a_%a_cup%%%%%%%

I removed the a_'s and left the front and back % signs, and did not get the expected result...

You can't remove the a_'s - the example is highly dependent on them. They are what turn the inner expansion into a valid variable name so that the next level of expansion can work.

Here is the example with added parentheses to help show how it is expanded (working from the inside out)

Code: Select all

::initial statement to be parsed
::parentheses added for emphasis, not part of code
call call echo:%%%%a_(%%a_(%a_cup%)%%)%%%%

::after initial parse: %a_cup%->pot, %%->%, %%%%->%%
call call echo:%%a_(%a_pot%)%%

::after first call: %a_pot%->box, %%->%
call echo:%a_box%

::after 2nd call: %a_box% -> crate
echo:crate

Final result: crate is echoed



Acy Forsythe wrote:I'm hoping that figuring it out will help me figure this out:

Code: Select all

:: I want to fix this line because it doesn't work:

set line=%%line%%space%:~0,80%


I personally feel like cmd.exe is just being stubborn and pig headed by not parsing that line the way I want it to. The result I was expecting is for it to act like this:

Code: Select all

set line=%line%%space%
set line=%line:~0,80%

You can't do what you are attempting in one line of code. You are attempting to concatenate two strings and then take a substring of the result.

The substring operation must be applied to a variable name. It cannot be applied directly to the target string.

Your solution is your last two lines of code.

Orange_batch's examples are different. In his code each inner expansion is resulting in the name of a variable that gets expanded as part of the next CALL.

Dave Benham

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: String Manipulation examples...

#23 Post by orange_batch » 17 Jun 2011 17:12

Haha, the topic of such a heated study, I'm honoured.

Acy Forsythe, just syntax errors. I'll explain what's going wrong. Your code:

Code: Select all

set line=%%line%%space%:~0,80%

Command interpreter sees the following variables in bold/underline:
set line=%%line%%space%:~0,80%

What you want is what you already wrote...

Code: Select all

set line=%line%%space%
set line=%line:~0,80%

It actually has nothing to do with calls or dynamic variables.

As for my thread though, this is what's happening:

Code: Select all

call call echo:%%%%a_%%a_%a_cup%%%%%%%

It's expanding "from the inside out". First, Command Prompt expands each pair of %%s. So...

call call echo:%%%%a_%%a_%a_cup%%%%%%%
...becomes (match the series of colours)...
call call echo:%%a_%a_pot%%%

...then it does the first call...
call echo:%%a_%a_pot%%%
...becomes (match the series of colours)...
call echo:%a_box%

...and the last call...
call echo:%a_box%
...becomes...
echo:crate

And so on. If I made each variable, like a_cup=a_pot rather than a_cup=pot, I could remove the a_'s separating each expansion and just keep re-expanding the result.

Code: Select all

set a_cup=a_pot
echo:%a_cup%

set a_pot=a_box
call echo:%%%a_cup%%%

set a_box=a_crate
call call echo:%%%%%%%a_cup%%%%%%%

Because the Command Interpreter converts %% to % (that is, 2 to 1) we work backwards exponentially, by doubling the number of %s for each later expansion.

More colour-coding so you can see...

set a_cup=a_pot
echo:%a_cup%

set a_pot=a_box
call echo:%%%a_cup%%%

set a_box=a_crate
call call echo:%%%%%%%a_cup%%%%%%%

Or perhaps more accurately...

set a_cup=a_pot
echo:%a_cup%


set a_pot=a_box
call echo:%%%a_cup%%%

set a_box=a_crate
call call echo:%%%%%%%a_cup%%%%%%%

...hope this is getting through lol.

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: String Manipulation examples...

#24 Post by Acy Forsythe » 18 Jun 2011 08:34

It's sinking in guys :)

You should have seen me yesterday trying to expand that line though I lost count of %'s :)

I did miss a very important part of your script that you just pointed out... I saw your variables as a_cup, pot, box, crate...

Orange_batch's examples are different. In his code each inner expansion is resulting in the name of a variable that gets expanded as part of the next CALL.


That's what I didn't catch that threw me off.

If feel better now that I know it can't work that way. I thought substring would work with a literal string...

Post Reply