Posts: 652
Threads: 96
Joined: Apr 2022
Reputation:
22
03-31-2023, 11:56 PM
(This post was last modified: 03-31-2023, 11:57 PM by PhilOfPerth.)
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.
Posts: 3,965
Threads: 176
Joined: Apr 2022
Reputation:
219
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 + ...
Posts: 1,272
Threads: 119
Joined: Apr 2022
Reputation:
100
(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().
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 652
Threads: 96
Joined: Apr 2022
Reputation:
22
Ah, of course it is! Sorry.
Posts: 652
Threads: 96
Joined: Apr 2022
Reputation:
22
(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.
Posts: 1,586
Threads: 59
Joined: Jul 2022
Reputation:
52
04-01-2023, 02:44 AM
(This post was last modified: 04-01-2023, 02:47 AM by mnrvovrfc.
Edit Reason: Sorry didn't read, it's a whole fragment and not just a char
)
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.
Posts: 2,696
Threads: 327
Joined: Apr 2022
Reputation:
217
Posts: 208
Threads: 13
Joined: Apr 2022
Reputation:
52
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
|