Why is TOT~4 legal as a string but NOT as a numerical value?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Why is TOT~4 legal as a string but NOT as a numerical value?

#1 Post by alan_b » 07 Dec 2013 17:32

Code: Select all

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "V~1=123"
SET "V~2=456"
SET /A TOT_3=%V~1%+%V~2%
ECHO %V~1%+%V~2% = %TOT_3%
echo pre-missing operator
SET /A TOT~4=%V~1%+%V~2%
echo post-missing operator
ECHO %V~1%+%V~2% = %TOT~4%
GOTO :EOF

The above code produces this result
    C:\Users\Alan>xc
    123+456 = 579
    pre-missing operator
    Missing operator.
    post-missing operator
    123+456 =

    C:\Users\Alan>

That demonstrates that '~' can be used when defining a string variable but not a numerical value.
I am working on something in which it would be convenient to compute a variable "TOT~4"

Do I need to compute variable TOT_4 and then assign its value to a String variable TOT~4
or is there a simple way to escape the evil poison with which '~' has surprised me ?
(I have vague memories of escaping poison with ~~ or ^~ or some such thing)

Regards
Alan

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Why is TOT~4 legal as a string but NOT as a numerical va

#2 Post by foxidrive » 07 Dec 2013 18:51

alan_b wrote:

Code: Select all

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "V~1=123"
SET "V~2=456"
SET /A TOT_3=%V~1%+%V~2%
ECHO %V~1%+%V~2% = %TOT_3%
echo pre-missing operator
SET /A TOT~4=%V~1%+%V~2%
echo post-missing operator
ECHO %V~1%+%V~2% = %TOT~4%
GOTO :EOF

The above code produces this result
    C:\Users\Alan>xc
    123+456 = 579
    pre-missing operator
    Missing operator.
    post-missing operator
    123+456 =

    C:\Users\Alan>

That demonstrates that '~' can be used when defining a string variable but not a numerical value.


You will see in SET /? that ~ is a unary operator in set /a

Code: Select all

! ~ -               - unary operators

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Why is TOT~4 legal as a string but NOT as a numerical va

#3 Post by alan_b » 08 Dec 2013 07:08

Thanks

I now understand.

Regards
Alan

Post Reply