@SUBST ... how replace character with a caret?

Nov 2, 2008
187
1
#4
What seems to be happening is that subst is seeing the string ^^ or ^& as two characters, eg subst[2,^^,foobar] gives fo^ar, while subst[2,^&,foobar] gives fo&ar. This comes by that subst[2,^&,foobar] is actually returning fo^&ar, and this is later expanded to fo&ar.

This works around it. You can use any character after the first /E, that is not likely to appear in your headings. %= accesses the escape character, and a second setdos will restore it to its default. For some generic batch, you could get away with an environment variable.

setdos /E£ & echo %@subst[2,^,foobar & setdos /E^

gives fo^bar

If you don't want to use the second setdos, you could nest the command in a setlocal / endlocal.
 

rconn

Administrator
Staff member
May 14, 2008
10,551
97
#5
This one behaves rather oddly:

Code:
v:\> echo %@subst[3,%%@char[94],foobar]
foo^
What happened to the rest of the string?
It was overwritten, as you specified.

@SUBST is an overwrite, not an insertion. What you passed to @SUBST was "3,%@char[94],foobar". So this translates to :

Write the string "%@char[94]" to the string "foobar", starting at position 3. The result is "foo%@char[94]".
 
#8
On Sun, 27 Feb 2011 09:53:05 -0500, rconn <> wrote:

|SETDOS /X-8 is your friend.

Yeah! It has been 20 years since I started with 4DOS and it never fails ...
just when I **need** SETDOS, I have forgotten about it. Thanks for the
reminder.
 
#9
---- Original Message ----
From: rconn
To: [email protected]
Sent: Sunday, 2011. February 27. 09:39
Subject: RE: [Support-t-2635] Re: @SUBST ... how replace character with
a caret?

| Quote:
| Originally Posted by vefatica
| This one behaves rather oddly:
|
|
| Code:
| v:\> echo %@subst[3,%%@char[94],foobar]
| foo^What happened to the rest of the string?
|
| It was overwritten, as you specified.
|
| @SUBST is an overwrite, not an insertion. What you passed to @SUBST
| was "3,%@char[94],foobar". So this translates to :
|
| Write the string "%@char[94]" to the string "foobar", starting at
| position 3. The result is foo%@char[94].

@SUBST[n, string1, string2]: Substitutes string1 starting at position n in string2.

F:\JPSOFT>*for /l %n in (0,1,9) echo %n %@subst[%n,x,0123456]
0 x123456
1 0x23456
2 01x3456
3 012x456
4 0123x56
5 01234x6
6 012345x
7 0123456x
8 0123456x
9 0123456x

Using "xy" as the replacement string has similar result - 2 consecutive characters are replaced with "xy". Based on the above result, I'd like a more explicit explanation of "boundary conditions" (i.e., when position n is not part of the string). And I still do not see why replacing a single character in the string with the caret ^ truncates the string?
--
Steve
 

rconn

Administrator
Staff member
May 14, 2008
10,551
97
#10
Using "xy" as the replacement string has similar result - 2 consecutive characters are replaced with "xy".
WAD.

Based on the above result, I'd like a more explicit explanation of "boundary conditions" (i.e., when position n is not part of the string).
It's appended to the end of the string. But the user bears some responsibility to not pass garbage!

And I still do not see why replacing a single character in the string with the caret ^ truncates the string?
It never does -- Vincent wasn't replacing a single character, he was overwriting the string with the literal string "%@char[94]" -- NOT the result of the @char.