Why is this function getting these results in these circumstances?

#1
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
 
#3
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
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,575
46
Albuquerque, NM
prospero.unm.edu
#4
Your unbalanced parentheses are confusing the parser; see this thread.

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
 
#5
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
 
#6
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"...
 
#7
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 was teaching C++ at a “business” school using materials that I had developed, and the owner of said institution found out that Microsoft was offering a C++ certification test and he enrolled me in same and paid for me to take it. He thought saying that I was “Microsoft Certified” was a good advertising “theme”, and he also wanted me to use the “official” Microsoft-endorsed materials that Microsoft would then supply rather than the materials I had developed on my own; and I had absolutely no objection to any part of that. So I flew down to Atlanta, Georgia, where the test was being administered by a Microsoft employee and took it. Well, it was a 2-hour test that I finished in about 45 minutes, and the next day the Microsoft employee who had administered the test called me in Chicago from wherever he was in Washington state to tell me that “he and apparently many other people in the room because they had mentioned it on their way out had thought that I had given up on the test when I walked out after 45 minutes, when, in fact, I tied for the 2nd-highest score ever up to that point in time.” He then went on to tell me that “all of the questions I had missed were easy” in his opinion, and if I had “spent another ten to fifteen minutes going over my answers I would have had the first perfect score ever up to that point in time.” (Now I will add that he many not have actually been correct in that supposition; for me there tends to be an inverse relationship between how “easy” the question is and how likely I am to get it right for whatever reason(s).) Just as some additional notes regarding this subject: First, it probably took me a whole year to learn C++ well whereas I had probably learned all previous computer languages I know in two or three days, max. In fact, in my first programming job ever I started writing IBM Series 370 mainframe assembler code the first day I was on the job; and prior to walking in the door to that building on the first day of the job I’m not sure that I had ever even heard of an IBM Series 370 mainframe. (Just to be clear here, I wasn’t all that productive in that capacity the first week or so I was on the job; but I was functioning.)

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.

The “autobiography” (note that you really should skip to the bottom if you get “tired” of the stuff at the top so you know that there’s another “side of the coin” and, believe me, I am not and never was “superhuman”): So, to start, I'm really not trying to brag here, but when programming type things were my career before I became too disabled to work, speed and efficiency were hallmarks of my efforts, as well as a complete lack of bugs. There are only two instances in my entire career where anybody found a bug in code after I had delivered it; and in one instance I had only been given instructions for what the program was supposed to do verbally and I had misunderstood them; the program worked perfectly but the results were not what were wanted. And ironically, this came up with one of my friends at dinner this week where he was a manager and had to do manage the whole system "life cycle" thing as a large part of his job; formal specifications, formal design, meetings for "walk trough’s" of the previous and formal testing (by a department of his company whose entire job was the formal testing of the programs written in all parts of the organization). Well I worked at the organization that he does which is how we met, and I was never subjected to any of the previous, ever; and it was his observation that that was because I was primarily a "tool smith" who created utilities that were useful to the programmers writing the code as a whole and/or relatively complex functions that returned a (sometimes small set of; so they weren't always "functions" exactly) results either because of the again, complexity, of the code involved and/or this was something that was used fairly commonly throughout the code; and I actually wrote was what called an "SVC" (supervisor call - a routine that ran in "privileged" mode on an IBM mainframe) that could do things that “ordinary” programs were not allowed to do because they could "mess up" the programs of other users that were running concurrently or even the entire operating system as a whole. IBM mainframes had "privileged" and "non-privileged" levels that were strictly enforced by the hardware, there were actually a small number of instructions in the instruction set that could not be executed at all by non-privileged programs for "safety" reasons. However, I will note that there were a few instances where I was tasked to write code that actually needed to use those instructions (I will note, not to be too mysterious here, that the primary time that this happened was in a situation where code that was written in the home office was distributed to about 20 (as I remember) “regional” offices that each did all of the data processing for their region rather than that all being done by the computers in the “home” office, and this was a very large system written by probably a dozen programmers or so that did all kinds of things related to auditing (which was a legal requirement) and verification that what was running in the individual regional offices what was should be running in the regional offices as well as deliver, do formal “repairs” and even “back outs” of code and data that had been sent to the regional offices. This system required access to data that ordinary “mortal” programs were not allowed to have for “security” reasons as well as direct access to the communication lines between the home and regional offices. I, individually, (designed! and) wrote all of the code that was used for these purposes with basically no “interference” from management whatsoever other than telling me that “here is a specific need that needs to be fulfilled”; it was entirely up to me as to how that need was met.) And I was never, in my entire career after my first data-processing job, subjected to any part of the whole “life cycle” rigmarole, ever. And I never personally met any human being in my entire career that was better than me; it just never happened. But I want to note several more things here that are kind of on the “other side of the coin” here. Number one, Rex is the only data processing person whose code I have personal experience with who is as good and quite likely better than I was. Number two, while I have no individual experience with any of their work (other than “plug-ins”), it is my distinct impression that there are a number of people who regularly post on this bulletin board, quite possibly including you, who are in the same general category that I was in, and number three and maybe most importantly, this was the first and only thing that I ever found in my entire life that I was actually good at; I was quite marginal if not actually bad in almost everything else; and since my memory was never really “good” (it’s God-awful now) that may have been a large part of the reason why.

And that’s the first of the many things on the “negative” side, and I’ll begin saying that I am single and have never married nor had children. This is because I made the explicit decision before the age of 10 to not have children, neither biological nor adopted; and I have completely “stuck to it”. It would be natural to wonder why a 9 year old (or maybe younger! I’m not really sure) would make a decision of that nature, and the reasons were many and all relatively simple. So, going through them one-by-one in no particular order, I was legally blind at the age of 7 (an ophthalmologist I consulted when I was a kid once told me “I have seen people with slightly worse nearsightedness than you have and people who have slightly worse astigmatism than you have, but I’ve never seen anybody who even close to you in terms of having both." And he had probably been practicing for 40 years I would guess when he told me that). My memory has never been good; it’s just that it wasn’t incredibly bad earlier in my life. I had a large number of significant allergies. I was short for my age and hit puberty late; I have a 2nd grade class picture where we are lined up by size (not too surprisingly) and I’m at the very end of the first row because I was, I would guess looking at the picture, about 2 inches shorter than the shortest girl. I was extremely clumsy and uncoordinated. I had an extremely late puberty (I just started puberty as a senior in high school and the bulk of it occurred when I was a freshman in college) which wasn’t helped by the fact that my mother had put me in school a whole year earlier than what was recommended. And, finally and by far the most personal but for whatever reason I’m “laying my cards on the table” here, there is that fact m father had a nasty, violent, temper and it appeared at an early age that I might have the same and that I was not going to subject children, adopted or biological, to what I was being subjected to as a child. And I’m going make this clear here: that actually was an explicit decision and there was no possibility whatsoever that I would ever change my mind; I’ve regretted very much that I had to make that decision; I’ve never regretted for even a microsecond that I made that decision; as far as I was concerned when I made the decision and still think today it was the morally correct decision – I was not going to have children that inherited what I inherited nor was I going to subject them to what I had been subjected to as a child. And I will add that I learned very early on in my life to tell this to the women in my life that I dated fairly early in the relationship; and in all cases the “dating” ceased as of that moment. I will add, however, that a number of these women are still close friends of mine (although most long ago married other people had children who are now adults), and these are all relationships that have lasted 20 years or more.

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

- Dan