VALID
                                    =====

Test whether a variable is valid as a given type


Usage:
------

        RETurn VALID%(expected_type, variable)

where expected_type is:

        0 = long integer, 1 = string, 2 = floating point, 3 = integer.

A further "type" exists: -1 = query

If the variable, literal or expression does not conform to the expected
type the function returns FALSE, otherwise it returns TRUE. An
additional bonus of this function is that it returns FALSE if the variable
is unset ie, has not been given any value. (This is not true for the
SMSQ/E version of SuperBASIC, SBASIC, though. But then it doesn't fall
over when it hits an unset variable either But see below for other ways).

If you are in doubt about how VALID% interprets its input, try it out in
S*BASIC first:

        x% = 'abc' is the same as VALID%(3, 'abc'),

ie BANG! for S*BASIC and 0 for VALID%

        x% = '5+2': PRINT x%

prints 5, not 7!

So dont assume that VALID%(3, '5+2') has validated your function! It would
approve of VALID%(3, '3/0') too! Try

        x% = '3/0': PRINT x%

But VALID% will not approve VALID%(3, 32768), because 32768 is not a valid
integer. Try

        x% = 32767

and then

        x% = 32768

Or try

        PRINT VALID%(2, '1e999')

1E999 is not a valid float under Qdos/SMSQ. Valid floats only go to about
+/-1E616.


This makes it possible to write procedures in SuperBasic with optional
parameters, eg

        DEFine PROCedure Test1(a$, b, c%)
        IF NOT VALID%(3, c%): c% = default%
        PRINT a$,b,c%
        END DEFine Test1

Set default% to whatever you like and try

        Test1 '1',2

or
        Test1 1,2,3

or
        Test1 '1',2,'rubbish'.

(SBASIC requires a slight modification).


A variation on VALID% is query:

        RETurn VALID%(-1, variable)

returns the type of the parameter variable as an integer. It does not tell
you whether the contents of a variable are valid or not eg whether
a$='.-3' is a valid number, as you can using the other type specifications
above, but it will tell you the basic type of the variable in question.
These are the types that can be tested (values in hex and decimal):

$0001 (1) unset str   $0201 (513) set str   $0301 (769) str array
$0002 (2) unset fp    $0202 (514) set fp    $0302 (770) fp  array
$0003 (3) unset int   $0203 (515) set int   $0303 (771) int array

also:

$0602 (1538) REPeat loop index   $0702 (1794) FOR loop index
$0300 (0768) Sub string

So how to use this capability? Well, to demonstrate how this works:

100 DEFine PROCedure Test2(v)
110 v$ = HEX$(VALID%(-1, v), 16)
120 v% = v$(2):         REMark Usage
130 SELect ON v%
140  = 0: PRINT 'This variable is unset!': RETurn
150  = 3: PRINT 'We dont do arrays!': RETurn
160 END SELect
170 :
180 v% = v$(4):         REMark Type
190 SELect ON v%
200  = 1: PRINT 'This is a string'
220  = 2: PRINT 'This is a float'
225  = 3: PRINT 'This is an integer'
230 END SELect
240 END DEFine Test2
250 :

To try it out, try:

10 Test a
20 a$ = 'abc': Test2 a$
30 DIM a%(10): Test2 a%
40 Test2 a%(0 TO 0):    REMark Is this still an array?
50 Test2 b%: b% = 1:    REMark b% hasnt been reached on the first run!
60 :

So, as you can readily see, VALID% could be used to create universal
functions that act differently on different inputs.


You can also tell which separator (ie , ; ! \ TO) variable is followed by,
or whether it is preceded by a hash -

        PRINT VALID%(-1,#2;)

but what's the point?


Software status:
----------------

Two versions of VALID% are included here:

One compact, all-inclusive toolkit, VALIDi ( _BIN and _asm), as per the
article on Knoware. Use this toolkit if the function is to be used on its own.

The second version has been "modularised" so it can easily be made part of
a pick 'n mix toolkit. It appears bulkier, as it uses libraries for routines
that may be common to many different commands.


V0.01, © PWITTE, 1992
V0.02, pjw, June 23rd 2000, bug fix
V0.03, pjw, June 20th 2019, All platforms (two formats)
V0.03, pjw, 2021 Oct 19, Only updated this text

               Conditions of use and DISCLAIMER as per Knoware.no

Generated by QuickHTM, 2022 Jun 30