SuperBASIC function for type validation

Anything QL Software or Programming Related.
User avatar
grafvonb
Bent Pin Expansion Port
Posts: 80
Joined: Sat Jan 05, 2019 7:54 am
Location: Frankfurt/Germany

SuperBASIC function for type validation

Postby grafvonb » Thu Jul 09, 2020 7:27 am

I can't find it :(. How to check if a non-numeric of empty data has been entered in a numeric field (or vice versa) using SuperBASIC?


Loving QL & ZX Spectrum & Amiga...
User avatar
Artificer
Bent Pin Expansion Port
Posts: 99
Joined: Fri Nov 24, 2017 8:43 am

Re: SuperBASIC function for type validation

Postby Artificer » Thu Jul 09, 2020 8:58 am

Hi,

Are you looking for a function/procedure in Superbasic to ensure only numeric input? If so there are a few examples in QL World Magazine.

In QLWorld 1986 November 1986 page 36 there is a Superbasic function. The magazine can be downloaded from Dilwyn Jones site here http://www.dilwyn.me.uk/mags/qluserworld/QLWorld_1986-11.pdf

There was also a calculator project later on perhaps in 1988.

Cheers


User avatar
tofro
Font of All Knowledge
Posts: 2015
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: SuperBASIC function for type validation

Postby tofro » Thu Jul 09, 2020 10:35 am

There are quite some ways to skin that cat:

The S*Basic reference manual has another nice example of a "safe input" routine in its INPUT section that doesn't need any Toolkit.

With some "modern" toolkits/compilers you can make this significantly simpler.

WHEN ERROR: Catch the "bad parameter" error on coercion and repeat input

DIY Toolkit has some functions (CHECKF and CHECK%) that test whether a string can safely be converted to a number.

Turbo Toolkit has functions EDITF and EDIT%, which will reject anything that is not a proper number.


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
NormanDunbar
Forum Moderator
Posts: 1124
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: SuperBASIC function for type validation

Postby NormanDunbar » Thu Jul 09, 2020 11:03 am

Code: Select all

1000 WHEN ERRor
1010    PRINT "That was not a number!"
1020    CONTINUE
1030 END WHEN
1040 :
1050 :
2000 REPeat loop
2010    INPUT "Please enter a number: "; num$
2020    num = num$
2030    PRINT "Your number was: " & num
2040 END REPeat loop


The above sort of works. Empty strings or anything not starting with a digit cause the error handler to fire up and tell you off. It converts anything that starts with a digit to a number up until the first non-digit character. "123x" will return the value 123 and not cause an error.

Number formats such as "1e6" don't fire the error handler, but return 1e6 as the value. Floating point numbers are happily accepted as well.

HTH

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890
User avatar
NormanDunbar
Forum Moderator
Posts: 1124
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: SuperBASIC function for type validation

Postby NormanDunbar » Thu Jul 09, 2020 11:05 am

Beaten, once again, by tofro. :(


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890
User avatar
tofro
Font of All Knowledge
Posts: 2015
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: SuperBASIC function for type validation

Postby tofro » Thu Jul 09, 2020 11:07 am

NormanDunbar wrote:Beaten, once again, by tofro. :(


No worries, Norm, I'll hold back next time ;)

At least you've presented an example that works in SBASIC - I haven't.

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
NormanDunbar
Forum Moderator
Posts: 1124
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: SuperBASIC function for type validation

Postby NormanDunbar » Thu Jul 09, 2020 11:17 am

Nah, don't hold back, I have been known to be wrong! Just ask my wife!

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890
User avatar
grafvonb
Bent Pin Expansion Port
Posts: 80
Joined: Sat Jan 05, 2019 7:54 am
Location: Frankfurt/Germany

Re: SuperBASIC function for type validation

Postby grafvonb » Thu Jul 09, 2020 11:21 am

Many thanks guys, I have now a lot of stuff to check!


Loving QL & ZX Spectrum & Amiga...
User avatar
pjw
Super Gold Card
Posts: 698
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: SuperBASIC function for type validation

Postby pjw » Thu Jul 09, 2020 12:48 pm

Ive never been fond of WHEN ERRor. I just use it as a last resort. Perhaps this is because of all the problems the various implementation had in their early incarnations. But even now there are tricky issues to beware of. Viz:

Code: Select all

1000 WHEN ERRor
1010    PRINT "That was not a number!"
1020    CONTINUE
1030 END WHEN
1040 :
1050 :
2000 REPeat loop
2010    INPUT "Please enter a number: "; num$
2015    IF num$ = '': EXIT loop
2020    num = num$
2030    PRINT "Your number was: " & num
2040 END REPeat loop
2050 :
2060 num = 5/0: REMark Just a trivial example


The problem being demonstrated is that the next error encountered will (probably unintentionally) trip the same WHEN ERRor clause. Ones needs to remember to turn WHEN ERRor off:

Code: Select all

1000 WHEN ERRor
1010    PRINT "That was not a number!"
1020    CONTINUE
1030 END WHEN
1040 :
1050 :
2000 REPeat loop
2010    INPUT "Please enter a number: "; num$
2015    IF num$ = '': EXIT loop
2020    num = num$
2030    PRINT "Your number was: " & num
2040 END REPeat loop
2050 :
2052 :
2055 WHEN ERRor
2057 END WHEN: REMark WHEN ERR turned off
2058 :
2060 num = 5/0


Per
Be wary of large West-Coast corporations bearing gifts!
- Ancient Trojan proverb
User avatar
NormanDunbar
Forum Moderator
Posts: 1124
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: SuperBASIC function for type validation

Postby NormanDunbar » Thu Jul 09, 2020 1:11 pm

Hi Per,

good point, well made. Thanks.


Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890

Who is online

Users browsing this forum: No registered users and 7 guests