# Why is this function getting these results in these circumstances?

#### mathewsdw

I'm not going to call this a "bug" because there's a good possibility that I am not understanding something here. And to illustrate the problem I am having I will show the contents of an otherwise-silly .btm file that I wrote just to isolate the problem and the output from same. So, first, here's the .BTM file (named "ShowProblem.btm"):
Code:
``````@Echo Off
SetLocal
Function IncorrectSample=`%@If[%1 LT 0,(]%@Eval[%1]%@If[%1 LT 0,)]`
@Echo %@IncorrectSample[1]
@Echo %@IncorrectSample[-1]
Function IncorrectSample=
EndLocal
Quit 0``````
As you see can see, all it does is define a function named "IncorrectSample" and show the results of evaluating it with the values of plus and minus one.

Here's the result of running the above batch file:
Code:
``````[Z:\]ShowProblem
1
(%@Eval[-1]%@If[-1 LT 0,)]``````
I don't think I really need to elaborate on what the problem is.

I will note that after much experimentation I found that changing the "%@If[%1 LT 0,(]" to "%@If[%1 LT 0, (]" (a space between the comma and the open parenthesis, no caret needed) fixes the problem; and I can get the results I expect by wrapping the whole expression in "@Trim". But what I'd like to understand is why I have to do that.

- Dan

Code:
``Function IncorrectSample=`%@If[%1 LT 0,(]%@Eval[%1]%@If[%1 LT 0,)]```

I can't fathom what you're trying to do with that function definition. Can you explain it to me simply?

Charles,

In the "real" function, the "%@Eval[%1]" is a fairly long and complex series of separate arithmetic expressions that are separated by words describing the results of the individual expressions, and when the input argument is negative I want the output to be "-(results of all of the individual expressions with their descriptions)" (whereas if the value being tested is positive the output should be just "results of all of the individual expressions with their descriptions"). My posting was just a greatly "stripped down" illustration of the problem I am (was? It's got a workaround.) having.

- Dan

Here's one way to accomplish what you're trying to do:
Code:
``Function IncorrectSample=`%@If[%1 LT 0,(%@Eval[%1]),%@Eval[%1]]```

Note that the parentheses are balanced, and the @IF has three arguments.

If you're using SafeChars, you can also use %_OPENPAT and %_CLOSEPAT in lieu of the parentheses:
Code:
``````@Echo Off
SetLocal
Function IncorrectSample=`%@If[%1 LT 0,%_OpenPat]%@Eval[%1]%@If[%1 LT 0,%_ClosePat]`
@SafeEcho %@IncorrectSample[1]
@SafeEcho %@IncorrectSample[-1]
EndLocal
Quit 0``````

Charles,

Since the contents of real "@Eval" are quite long and complex doing it twice (particularly since both sides of an "@If" are always evaluated whether the "condition" is true or false) is really not an acceptable solution. However, while I'm answering this without really having tried it, I would guess that the "@SafeChars" solution is the way to go, particularly since I "live" in the 8-bit world (I experimented using 16-bit characters for a while; for my purposes it was just a pain in rear with no advantages whatsoever). So, as always, thank you!

- Dan

Charles,

Since the contents of real "@Eval" are quite long and complex doing it twice (particularly since both sides of an "@If" are always evaluated whether the "condition" is true or false) is really not an acceptable solution...

Dan:
I have many custom functions (UDFs) which execute batch files to get their job done. The batch file may have many ECHO commands (not redirected!), but it must execute exactly ONE; the line it send to STDOUT is captured by @EXECSTR and returned as the function value. A typical definition is below:

function UDF_by_batch=`%@execstr[(UDF_batch-file %\$)]`

(assumes your ParameterChar is the dollar sign \$, the TCC default - not used on my systems).

You may find this method to your liking (esp. with the batch file on a virtual disk with no access latency). Among other things, 1/ you can save the result of an expression so it would only be evaluated once; 2/ you can use IFF/ELSEIFF/ENDIFF to evaluate only those expressions which affect the result and skip the ones not required (which this way could even contain elements that would result in errors in other branches); 3/ do any of the many things available only in batch files.

Yet another method is to use an ALIAS instead of a batch file; you can do 1/ and 2/ above, and even use "inline DO"...

Steve, thank you for your long and thoughtful response, but it entirely misses the point of what I am trying to do:
Code:
``@Echo %@If[condition1,result1]%@If[codition2,result2]%@Ifcodictoin3,result3]...``
where each "result" can be some combination of (relatively simple) arithmetic expressions and text with appropriate punctuation (some of the "results" may just be a comma, for example), but it's important to note that more than one (or, rarely, even all) of the conditions may be true. And, there was a circumstance where if one of the values was negative all of the arithmetic results would then be negative and sometimes in that circumstance the values of the arithmetic results were somewhat strange (maybe because the calculations like "%@Floor[expression %% value] were quite common; in fact, to give a relevant example of from where this is coming, one of the results could be something like "3 weeks and 4 days and 5 hours…" and if that parameter was negative the whole mess would be negative and I found it better to produce something like "-(3 weeks and 4 days and 5 hours...)" vs. -3 weeks and -4 days and -5 hours and...) (This isn't exactly what I was doing, but it's very close, in principal). As far as @ExecStr goes, I probably have dozens of batch files that use it and @ExecArray also, but the "overhead" of actually creating an explicit "subtask"/"subshell"/whatever bothered me somewhat (please see the rather-long “autobiography” at the bottom of this posting as to why that was of real concern to me). At any rate, I recently “discovered” “Do … /P” somehow (I hadn’t been aware of it before then), and I find using it preferable to starting an explicit “subtask” using an “@Exec…:” function generally strictly for performance reasons if nothing else.

Now I’m going to to on more here on the “plus” side regarding my abilities in this area, and I will note that if anyone reading this doesn’t believe me it can probably be arranged for me to be able to refer you to “references” who will verify the truth of everything I say in this posting and I have absolutely no problem with doing that; and the way I would do that is to give you a list of references (six people I can think of right off of the “top of my head” at the moment) you can then contact to verify any or all of what I’m stating in this missive. Three of these people have personal, intimate, knowledge of my abilities; and the other three have somewhat less “intimate” knowledge but are fully aware of my reputation in this area if nothing else. However, I have to specifically ask them if I can have you contact them for this confirmation and I would only do this in a “private” conversation with you on this bulletin board rather than posting their phone numbers and/or e-mail addresses in a “public” place.

So continuing, I have taught many computer related courses over the years at many institutions, both business and educational; usually, but not always, as jobs “on the side”, and I had a (completely justified!) universal reputation for being very good. One of the business, rather than academic, institutions that I taught for would regularly advertise me as “the instructor who gets only 9’s and 10’s on his student instructor ratings.” I think that this may be because I had a very “deep” understanding of what I was teaching; it may be because my college education was in “Electrical Engineering” specializing in computer hardware design. (I went to the Rose-Hulman Institute of Technology in Indiana; “which for many years has been ranked No. 1 by U.S. News & World Report among institutions whose highest degree in engineering is the master's” (from http://www.rose-hulman.edu/about.aspx). I will note here, however, that the college has been “unranked” the last several times I actually looked, although the previous quote is still on their website.) I will note that I went to Rose on a full scholarship; I was a National Merit Finalist in high school. (Some things to add here that might be worth noting: #1, you actually only get a scholarship (i.e., money) from them if a parent works for a “participating” company; mine didn’t. #2. Being a “finalist” put me in the upper ½ of 1 percent of the population, but in the United States right now that’s more than a million and a half people at the present time.)

And I think that it’s worth noting here that Microsoft no longer administers that test; it is my guess that that is for the same reasons that they had developed and started administering it in the first place: C++ is a very difficult language and they developed the test to “weed out” the charlatans, particularly those who claimed to represent Microsoft in some way. However, I tend to believe that Microsoft eventually “gave up” on C++ altogether and developed C#, which is a much-simplified C++ subset from what I can tell (I’m not intimately familiar with C# because I’ve never had any need to be).

I will also note that I have "Certificate in Data Processing" that some companies were trying to promote a number of years ago for the same reason(s) that Microsoft introduced its C++ certification. I get the impression that that program was never really a success and is, if not completely, mostly dead. But I got said certification and got it by taking and passing all five tests (I had a perfect score on one of them) in a single day, which I had been advised not to do.

Well, that's more than enough, I think!

- Dan

Replies
6
Views
99
Replies
6
Views
121
Replies
6
Views
447
Replies
11
Views
517
Replies
6
Views
490
Documentation Function parameters
Replies
0
Views
388
Replies
0
Views
429
Replies
4
Views
850
Replies
3
Views
715
Replies
8
Views
1K
Replies
6
Views
1K
Replies
10
Views
2K
Replies
4
Views
1K
Replies
5
Views
1K
Replies
3
Views
1K
Replies
3
Views
1K
Replies
3
Views
2K
Replies
17
Views
2K
Replies
0
Views
983
Replies
10
Views
2K
Documentation @CAPI function summary
Replies
0
Views
886
Replies
3
Views
1K
Replies
3
Views
2K
Replies
10
Views
3K
Replies
8
Views
2K
Replies
5
Views
2K
Replies
13
Views
2K
Replies
2
Views
1K
Replies
10
Views
3K
Replies
2
Views
1K
Replies
5
Views
2K
Replies
15
Views
3K
Replies
13
Views
3K
Replies
1
Views
985
Replies
3
Views
2K
Replies
17
Views
3K
Replies
1
Views
2K
Replies
0
Views
1K
Replies
0
Views
1K
How to? user function
Replies
1
Views
1K
Replies
31
Views
6K
Replies
13
Views
2K
Replies
4
Views
2K
Replies
10
Views
3K
Replies
5
Views
2K
Replies
7
Views
2K
Replies
5
Views
1K
Replies
4
Views
2K
Replies
2
Views
1K
Replies
2
Views
1K