Welcome!

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

SignUp Now!

Any Workarounds for @if When False Case Generates an Error?

Jun
562
4
I have been wrestling with creating user-defined functions that use the @IF function to choose between two cases. As the help clearly points out, both the true and false cases are evaluated, so they must not generate an error. The function CLP that I showed in another post works only because the @CLIP function is amazingly tolerant of aberrant syntax and does not throw an error message for the alternative case. (Actually, I imagine that there are cases where the CLP function will not work; I just didn't come across them in my testing.)

Does anyone have a workaround for dealing with this?

The only thing I have come up with is to write a batch file, where the IF and IFF functions do not have the same issue, and to use the @EXECSTR function to call the batch file and get the result to be returned by the function.
 
Thanks, Scott. I guess I'll have to continue my education (which has been a lot of fun so far) to include library functions.
 
Thanks, Scott. I guess I'll have to continue my education (which has been a lot of fun so far) to include library functions.
Not much of a learning curve there. Write a BTM that does what you want. Decide on a name for the library function and put name { on a line of its own at the beginning and put } on a line of its own at the end. At this point the name of the file is irrelevant; you can't run it as a BTM so call it whatever you like. Below I used the .LIB extension. If it's in the "Library" subdirectory of TCC's home it'll be loaded automatically when TCC starts. Load it on-demand with LIBRARY /R filename, reload it with LIBRARY /R /U filename.

Here's one that converts a number of seconds to d:hh:mm:ss. It exports (from SETLOCAL) LIBRESULT for use later. It could as easily ECHO'd the result for capture later with @EXECSTR.

Code:
d:\data\tcclibrary> type s2dhms_source.lib
s2dhms {
setlocal
set _x=%1
set _d=%@eval[%_x\86400]
set _x=%@eval[%_x MOD 86400]
set _h=%@eval[%_x\3600]
set _x=%@eval[%_x MOD 3600]
set _m=%@eval[%_x\60]
set _s=%@eval[%_x MOD 60]
set libresult=%_d:%@format[02,%_h]:%@format[02,%_m]:%@format[02,%_s]
REM echo %libresult
endlocal libresult
}

d:\data\tcclibrary> s2dhms 100000

d:\data\tcclibrary> echo %libresult
1:03:46:40

Library functions are no more powerful than BTMs. They're just memory-resident. I once tried referring to %0 in one. That didn't work and could be considered a (very small) disadvantage.
 
Back
Top