Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

btm file command line augments comparing for string or math.

i've tried to research it especially in the help file but i'm not getting it.
i'm thinking somewhere between numerical and eval lies my answer but i'm still not getting it.
I get close..but not right.

I'd like to compare the command line augments to see if it's a mathematical equation and if so echo %@eval the augments
and if it's not mathematic do a google search on the string..

i'm sure it's simple for you programmers and those long experienced with tcc.. but i can't figure it out.
can someone show me how it should be done? hopefully i'll then pickit up and understand it once i see it done.
 
Here is one way of doing what you want;
Code:
@setlocal
@echo off
:: TCC  24.01.41 x64   Windows 7 [Version 6.1.7601]

iff %# gt 0 then
  set mycalc=%$
else
  set mycalc=2019-1956/(10-2)
endiff

set theLen=%@len[%mycalc]
set TPunct=%@tpunct[%mycalc]
set TDigit=%@tdigit[%mycalc]
echo TPunct+TDigit: %@eval[%TPunct+%TDigit]
echo theLen       : %theLen
iff %@eval[%TPunct+%TDigit] eq %theLen then
  echo %mycalc contains only Numeric Digits and punctuation characters
  echo This can be used as a math function
  echo %%@eval[%%mycalc] = %@eval[%mycalc]
else
  echo The %%mycalc variable cannot be used as a math function
  echo Doing a Google search of %mycalc
  https://www.google.ca/search?q="%mycalc"
endiff

Example runs;
Code:
c:\users\jlc\utils>test.btm 2+5*12
TPunct+TDigit: 6
theLen       : 6
2+5*12 contains only Numeric Digits and punctuation characters
This can be used as a math function
%@eval[%mycalc] = 62

Another example run;
Code:
c:\users\jlc\utils>test 2 + 5 * 12
TPunct+TDigit: 6
theLen       : 10
The %mycalc variable cannot be used as a math function

c:\users\jlc\utils>test "2 + 5 * 12"
TPunct+TDigit: 8
theLen       : 12
The %mycalc variable cannot be used as a math function

Joe
 
How about expressions containing letters?
Code:
set r=4
echo %@eval[pi * r * r]

Muddying the waters even further, @EVAL happily accepts nonexistent variables; they evaluate to zero:
Code:
echo %@eval[Oliver Cromwell]

I think the only way to determine whether a string is valid input to @EVAL is to try passing it to @EVAL....
Code:
set ans=
( set ans=%@eval[%$] ) >& nul:
if defined ans ....
 
I think the only way to determine whether a string is valid input to @EVAL is to try passing it to @EVAL....
Only if you're OK with zero meaning invalid. Even utter garbage evaluates to 0.
Code:
v:\> set answer=%@eval[utter garbage]

v:\> set answer
0

I wonder if @EVAL even knows when it was given a non-expression. If it does, returning something appropriate, like "N/A", would be more useful than 0.
 
So, just pass everything to Google; let Google decide whether it's a math expression or not!

I've been messing with @URLENCODE. This function is fun to use because it can return percent signs. Have to use SETDOS to disable nested variable expansion:

Code:
@echo off
::   Google.btm
setlocal
setdos /x-4
start https://www.google.com/search?q=%@urlencode[%$]
endlocal
 
Wow. thanks for all the replies. Some is more complex than i expected... funny i had given up and also came up with the idea of giving it all to google but it wouldnt' pass the url correctly when i did 2+2 i guess the plus is some type of hypertext code and was unaware of the urlencode feature. I'm going to read some of this a few times. Thank you all very much.
 
So it looks like i'm going with joe's method and using urlencode.. that should do it perfectly i think. Thanks !

EDIT: yep works beautifully! and i learned a few things. also added that google.btm to my btm dir and aliased it with the letter g.
 
Last edited:
No, zero is valid. The expression is invalid if @EVAL fails, i.e. the command is not executed.
Huh? In my example,
Code:
set answer=%@eval[utter garbage]
the SET command was executed. Does that mean @EVAL succeeded ... that the expression is valid?

It's hard to get @EVAL to fail ... overflow ... divide by zero ... and those can come from expressions which are actually valid.
 
Huh? In my example,
Code:
set answer=%@eval[utter garbage]
the SET command was executed. Does that mean @EVAL succeeded ... that the expression is valid?

I think UTTER is valid (evaluates to 0, unless you set it to something else) and GARBAGE is ignored. It does the same with numbers:
Code:
C:\>echo %@eval[42 1000]
42

C:\>

But if it finds an operator, @EVAL will carry on:
Code:
C:\>set utter=6765

C:\>set garbage=4181

C:\>echo %@eval[utter / garbage]
1.6180339632

C:\>
 
I suppose for proper algebra-ness, adjacent numbers without an operator between them ought to be multiplied. But the parser for @EVAL must be a... challenging piece of code already.
 
I suppose for proper algebra-ness, adjacent numbers without an operator between them ought to be multiplied. But the parser for @EVAL must be a... challenging piece of code already.
I think that is only for single letter variable names (possibly with coefficients, subscripts, exponents). x = 2, y = 3, xy = 6, 4x = 8, 4xy = 24. I've never thought of two numbers separated by whitespace as indicating multiplication (or anything). And, of course, if you don't separate two numbers with something, you get either one number or garbage. For multiplication ...
Code:
(2)(3) = 2x3 = 2∙3 = 2 x 3 = 2 ∙ 3 = 6

but not 2 3 = 6 or even (2) (3) = 6
 
Back
Top
[FOX] Ultimate Translator
Translate