Sign in to follow this  
Followers 0
memoryoverflow

number recognition

5 posts in this topic

#1 ·  Posted (edited)

Having stumbled over the side effects of implicit type casting way too often, I'm going to check expected numbers that might come as strings explicitely before using them.

Here's where I don't see the obvious:

Local $i = 0, $asResult[3] = ['hex', 'NaN', 'dec']
Local $asTest = StringSplit('-123.45e+12|-.1|23.E-9|0.|+.0|  12|001 || |0xfedcba98|-|2l0|10O0|12,345.67|9e|e8|7-6|+ 5|4.3.21|9 8|7+e6|5e4.3|2f1|1.2e-', '|')
Local $sRxNaDec = '(?i)[^-+.e\d\s]|\..*\.|e.*[^-+\d]|\A[^-+.\d\s]|[^.\d\s]\z|[^\A\s]\s[^\z\s]|[^e\A\s][-+]'
Local $sRxIsHex = '(?i)0x[\da-f]{1,8}\z'
For $i = 1 To $asTest[0]
    ConsoleWrite(StringFormat('%15s  %s', $asTest[$i], $asResult[2 - StringRegExp($asTest[$i], $sRxIsHex) - StringRegExp($asTest[$i], $sRxNaDec)]) & '   ''Number(…)'' = ' & Number($asTest[$i]) & @crlf)
Next
Well, it works, but...

... isn't there a much simpler way, I just can't see atm?

edit: $sRxHex amended

Edited by memoryoverflow

(The signature is placed on the back of this page to not disturb the flow of the thread.)

Share this post


Link to post
Share on other sites



Having stumbled over the side effects of implicit type casting way too often, I'm going to check expected numbers that might come as strings explicitely before using them.

Here's where I don't see the obvious:

Local $i = 0, $asResult[3] = ['hex', 'NaN', 'dec']
Local $asTest = StringSplit('-123.45e+12|-.1|23.E-9|0.|+.0| 12|001 || |0xfedcba98|-|2l0|10O0|12,345.67|9e|e8|7-6|+ 5|4.3.21|9 8|7+e6|5e4.3|2f1|1.2e-', '|')
Local $sRxNaDec = '(?i)[^-+.e\d\s]|\..*\.|e.*[^-+\d]|\A[^-+.\d\s]|[^.\d\s]\z|[^\A\s]\s[^\z\s]|[^e\A\s][-+]'
Local $sRxIsHex = '(?i)0x[\da-f]{1,8}'
For $i = 1 To $asTest[0]
    ConsoleWrite(StringFormat('%15s %s', $asTest[$i], $asResult[2 - StringRegExp($asTest[$i], $sRxIsHex) - StringRegExp($asTest[$i], $sRxNaDec)]) & ' ''Number(…)'' = ' & Number($asTest[$i]) & @crlf)
Next
Well, it works, but...

... isn't there a much simpler way, I just can't see atm?

Yes, it can be difficult. This gets near

Local $i = 0, $asResult[3] = ['hex', 'NaN', 'dec']
Local $asTest = StringSplit('-123.45e+12|-.1|23.E-9|0.|+.0| 12|001 || |0xfedcba98|-|2l0|10O0|12,345.67|9e|e8|7-6|+ 5|4.3.21|9 8|7+e6|5e4.3|2f1|1.2e-', '|')
Local $sRxNaDec = '(?i)[^-+.e\d\s]|\..*\.|e.*[^-+\d]|\A[^-+.\d\s]|[^.\d\s]\z|[^\A\s]\s[^\z\s]|[^e\A\s][-+]'
Local $sRxIsHex = '(?i)0x[\da-f]{1,8}'
For $i = 1 To $asTest[0]

    ConsoleWrite(StringFormat('%15s %s', $asTest[$i], $asResult[2 - StringRegExp($asTest[$i], $sRxIsHex) - StringRegExp($asTest[$i], $sRxNaDec)]) & ' ''Number(…)'' = ' & _Number($asTest[$i]) & @CRLF)

Next


Func _Number($sN)
    $sN = StringReplace($sN, ',', '')
    $res1 = Execute($sN)
    If $res1 = '' and not isNumber($sN) Then
    Return 'NAN'
    Else
    Return Number($res1)
    EndIf

EndFunc ;==>_Number

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for the quick answer, Martin

While it doesn't exactly fit my choices of permissiveness (which are to be revisted, anyway - except for the evaluation of expressions, which I don't want to allow at this place), it's a good inspiration to look into the execute function, anyway.

And yes, those commas are another story. I thought about allowing them, but decided not to unless I process the locale settings, too (which I have postponed).

p.s. & ot: Just seen that the topic still displays 0 views :)

But at least Martin and I viewed it - how that?

Edited by memoryoverflow

(The signature is placed on the back of this page to not disturb the flow of the thread.)

Share this post


Link to post
Share on other sites

p.s. & ot: Just seen that the topic still displays 0 views :)

But at least Martin and I viewed it - how that?

when Jon makes changs to the forum, the counter refresh time gets set back to the default which I believe is every 45 minutes and he keeps forgetting to change them again. Very likely because he's too busy to worry about the minor details.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Thanks for bothering, George.

John has answered himself in the 'chat' sub forum. Think to remember something like default = 3 hours, but well... memory overflow... and it wouldn't have mattered that much for sure. Overlooking such minor details isn't surprising. I myself appreciate it the more, if someone who sees such things gives me a hint. (E.g. the 2nd pattern in my opening here has a flaw, forgot to check that '0x...' must be the start: '\A0x...')


(The signature is placed on the back of this page to not disturb the flow of the thread.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0