Is there an IsAlpha function in QB64PE?
#1
Is there an IsAlpha or IsNum (or equivalent) function in PE, to examine an input's type? If not, should there be? 
I couldn't see one in Help or the Wiki.
I know we can use Select Case etc., but it would be much more convenient if these functions were available.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Reply
#2
Quote:to examine an input's type


An input's type is determined by the variable that contains the value:

Code: (Select All)
Input "Enter a number"; aNum ' << single type
Print "A number"; aNum
Input "Enter a string"; aString$ ' << determines Type
Print "A string "; aString$
b = b + ...
Reply
#3
(03-31-2023, 11:56 PM)PhilOfPerth Wrote: Is there an IsAlpha or IsNum (or equivalent) function in PE, to examine an input's type? If not, should there be? 
I couldn't see one in Help or the Wiki.
I know we can use Select Case etc., but it would be much more convenient if these functions were available.

Functions like those are really handy (and necessary) for languages that don't rely on, or are very flexible with, variable types. Since QB64 requires a variable to have a type (integer, single, string, etc..) there would be no reason to have an IsAlpha or IsNum function. All variables created in QB64 without specifying a type default to SINGLE, therefore they are always "IsNum" so to speak.

If you want to test a string for a numeric value use VAL().
Software and cathedrals are much the same — first we build them, then we pray.
QB64 Tutorial
Reply
#4
Ah, of course it is! Sorry.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Reply
#5
(04-01-2023, 12:13 AM)TerryRitchie Wrote:
(03-31-2023, 11:56 PM)PhilOfPerth Wrote: Is there an IsAlpha or IsNum (or equivalent) function in PE, to examine an input's type? If not, should there be? 
I couldn't see one in Help or the Wiki.
I know we can use Select Case etc., but it would be much more convenient if these functions were available.

Functions like those are really handy (and necessary) for languages that don't rely on, or are very flexible with, variable types. Since QB64 requires a variable to have a type (integer, single, string, etc..) there would be no reason to have an IsAlpha or IsNum function. All variables created in QB64 without specifying a type default to SINGLE, therefore they are always "IsNum" so to speak.

If you want to test a string for a numeric value use VAL().
Yep, got it. Thanks Terry.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Reply
#6
https://qb64phoenix.com/qb64wiki/index.p...s_and_Subs

It would be pretty easy to invent BASIC functions out of the C library ones in "ctype.h" header file.

Otherwise:

Code: (Select All)
DIM character AS INTEGER, somestring AS STRING, someposition AS LONG
character = ASC(somestring, someposition)
SELECT CASE character
CASE 44 TO 46, 48 TO 57
'(is a number)
'would have to check if plus or minus is the first character
'would have to check if there is only one of plus, minus or period
CASE 65 TO 90, 97 TO 122
'(is a letter)
CASE ELSE
'(is neither number nor letter)
END SELECT

(Declares AS INTEGER instead of AS _BYTE or AS _UNSIGNED _BYTE because those pesky negative numbers would have to be clamped away and other such calculations.)

EDIT: Added "CASE ELSE" so it could be put inside a function which should have extensive checks for number's sake, since that is more demanding than doing it to an ordinary English word apart from @#%& or other such gibberish. Smile
Reply
#7
https://staging.qb64phoenix.com/showthre...ight=Isnum
Reply
#8
The ctype.h header is allready included in QB64pe and its functions can easily accessed via a declare library block.
Code: (Select All)
DECLARE LIBRARY
    'ctype.h
    FUNCTION isalnum% (BYVAL c AS INTEGER) 'is an alphabet letter(isalpha(c) or isdigit(c))
    FUNCTION isalpha% (BYVAL c AS INTEGER) 'is letter (isupper(c) or islower(c))
    FUNCTION isdigit% (BYVAL c AS INTEGER) 'is a decimal digit
    FUNCTION isgraph% (BYVAL c AS INTEGER) 'is a printing character other than space
    FUNCTION islower% (BYVAL c AS INTEGER) 'is a lower-case letter
    FUNCTION isprint% (BYVAL c AS INTEGER) 'is printing character. ASCII: &H20 (" ") to &H7E (~)
    FUNCTION ispunct% (BYVAL c AS INTEGER) 'is printing character other than space, letter, digit
    FUNCTION isspace% (BYVAL c AS INTEGER) 'is space, formfeed, newline, return, tab, vertical tab
    FUNCTION isupper% (BYVAL c AS INTEGER) 'is upper-case letter
    FUNCTION isxdigit% (BYVAL c AS INTEGER) 'is a hexdecimal digit character(0 thru 9 or A thru F)
END DECLARE

LINE INPUT "Gimme a char: "; c$

IF isalpha%(ASC(c$)) THEN
    PRINT "Is alpha."
ELSE
    PRINT "Is NOT alpha."
END IF

END
For more informations/other functions see https://qb64phoenix.com/qb64wiki/index.php/C_Libraries
Reply




Users browsing this thread: 2 Guest(s)