1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Function tips and tricks

Discussion in 'T&T - Functions' started by nchernoff, Jun 9, 2008.

  1. nchernoff

    nchernoff Administrator Staff Member

    Joined:
    May 16, 2008
    Messages:
    42
    Likes Received:
    1
    Migrated From JP Software Wiki

    Function tips and tricks is for short tips or non-obvious ways to define functions.

    User-defined "internal" variables

    Internal variables return information from the system, such as the current time. 4NT lets the user create user-defined functions but not user-defined internal variables. User-defined internal variables can be simulated by creating a parameterless function, e.g.:
    Code:
    :  Returns am if the current hour is less than 12, otherwise pm.
    :  Syntax: Echo %@ampm[]
    ampm %@if[%_hour lt 12,am,pm]
    Note two differences between a real internal variable and the simulated internal variable: (1) empty function brackets are required; (2) the variable is accessed like any other function, with %@functionname, not %_variablename.

    Alternatively, you can create a variable in the environment (with or without a preceding underscore as you prefer) and avoid the above limitations. For example:

    Code:
    Set _ampm=`%@if[%_hour lt 12,am,pm]`
    Functions with default or range-limited values

    Here is how to add a default value or range limits to a user-defined function. If the parameter is empty this will automatically substitute the current time. The comparison at the beginning could be changed to limit range, eliminate negative numbers, etc.
    Code:
    : Example of how to add a default value to a function
    alwaystime %@if["%1" ne "", %1, %_time]
    This technique more commonly would be done with nested %@if[] functions. For instance, combine this with @ampm so that if the user provides a time that time will determine whether am or pm is returned but if the user leaves the parameter empty the current time will be used. This would be particularly useful in log files or printouts that will sometimes be used by persons not comfortable with 24-hour time formats.

    Quotes for filenames

    Some functions or internal variables return filenames, pathnames, etc., for instance, @unique[] creates a file and returns the full pathname. But consider this code and assume it is executed in c:\program files\JPSoft\

    Code:
    set scratchfile=%@unique[]
    select echo (*.*)>>%scratchfile
    This is what this will do, (with certain names assumed):

    1. Create UN1.tmp in the current directory
    2. Set scratchfile to c:\program files\JPSoft\UN1.tmp
    3. Display a menulist
    4. Try to dump the selected file names somewhere in c:\program\

    To avoid this problem set scratchfile="%@unique[]"
    Note the addition of the double quotes.
     
    #1

Share This Page