Uten Posted December 3, 2006 Share Posted December 3, 2006 I'm probably in the stupid corner today so pleas excuse me if I have overseen the obvious. I want to make sure a variable contains a number so I can decide if I should do a numeric comparison or a character based one. My first try was to use number($arg) andIsNumeric($arg) but there are trouble in heaven number("21j") Will return 21 as explained in the help file. And IsNumeric($arg) is false if $arg is initiated with "21". So, I have created this function and validating tests. Can you do better? Using StringRegExp (like I have done) to validate if the argument seems like overkill to me Func NumNumeric($arg) ;PURPOSE: Check if argument is numeric. ;TODO: How about hex, oct ...(probably use flags)? Return NOT StringRegExp($arg, "[^\d]+") ;Number will return 0=false for 0 EndFunc Func assert($bool, $msg = "Unexpected result", $erl=@ScriptLineNumber) if not $bool then ConsoleWrite("(" & $erl & ") := " & $msg & @LF) EndIf EndFunc Func testNumNumeric() assert(NumNumeric(0)) assert(NumNumeric("0")) assert(NumNumeric(2)) assert(NumNumeric("2")) assert(NOT NumNumeric("21e")) assert(NOT NumNumeric("21j")) ConsoleWrite("+++DONE: testNumNumeric" & @LF) EndFunc Your input is appreciated Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
GaryFrost Posted December 3, 2006 Share Posted December 3, 2006 I'm probably in the stupid corner today so pleas excuse me if I have overseen the obvious. I want to make sure a variable contains a number so I can decide if I should do a numeric comparison or a character based one. My first try was to use number($arg) andIsNumeric($arg) but there are trouble in heaven number("21j") Will return 21 as explained in the help file. And IsNumeric($arg) is false if $arg is initiated with "21". So, I have created this function and validating tests. Can you do better? Using StringRegExp (like I have done) to validate if the argument seems like overkill to me Func NumNumeric($arg) ;PURPOSE: Check if argument is numeric. ;TODO: How about hex, oct ...(probably use flags)? Return NOT StringRegExp($arg, "[^\d]+") ;Number will return 0=false for 0 EndFunc Func assert($bool, $msg = "Unexpected result", $erl=@ScriptLineNumber) if not $bool then ConsoleWrite("(" & $erl & ") := " & $msg & @LF) EndIf EndFunc Func testNumNumeric() assert(NumNumeric(0)) assert(NumNumeric("0")) assert(NumNumeric(2)) assert(NumNumeric("2")) assert(NOT NumNumeric("21e")) assert(NOT NumNumeric("21j")) ConsoleWrite("+++DONE: testNumNumeric" & @LF) EndFunc SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference. Link to comment Share on other sites More sharing options...
Developers Jos Posted December 3, 2006 Developers Share Posted December 3, 2006 Did you look at StringIsDigit () ? SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Uten Posted December 3, 2006 Author Share Posted December 3, 2006 (edited) Thanks for your suggestions @gafrost and @JdeB. I just new my solution was on the far left and beyond.... I just did not have the imagination to check for other String* functions when I was looking for a number.:"> StringIsDigit() and StringIsInt() worked with my tests. EDIT: And there even is a StringIsAlNum(...) Edited December 3, 2006 by Uten Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
Uten Posted December 3, 2006 Author Share Posted December 3, 2006 Uh, Should not this return 0?? StringIsAlNum("21e") Even the sample in the help file is a bot odd. Or is it time to call it a day for me? Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
Uten Posted December 3, 2006 Author Share Posted December 3, 2006 Yup, definitely time for bed. I thought alphanumeric was only numbers but Merriam-webster says othervice. Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
xcal Posted December 4, 2006 Share Posted December 4, 2006 (edited) Try this:Dim $array[7] = [6, '12345', 67890, 'a12345', '12345b', '123c45', 'abcdef'] For $i = 1 To $array[0] MsgBox(0, $i & ' of 6', $array[$i] & ' = ' & _charcheck($array[$i])) Next Func _charcheck($var) If StringRegExp($var, '[:alpha:]') Then Return 'letters or mixed' Else Return 'numbers only' EndIf EndFuncedit - I just noticed that this does not work in 3.2.1.13, but it works perfectly fine in 3.2.0.1. Edited December 4, 2006 by xcal How To Ask Questions The Smart Way Link to comment Share on other sites More sharing options...
Uten Posted December 4, 2006 Author Share Posted December 4, 2006 Try this: .....You did not read the first post did you v3.2.0.1 Used the home brewed regexp engine. The latest beta +3.2.1.8 (I think) uses the pcre implementation. There has been some report of failling classes in the pcre implementation. You are encurraged to try the pcretest.exe on your regexp before complaining. Could be the reason it does not work. Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
xcal Posted December 4, 2006 Share Posted December 4, 2006 Hmm what first post? Where's that from? How To Ask Questions The Smart Way Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now