MilesAhead Posted January 1, 2012 Share Posted January 1, 2012 Basically, since 0 is a valid integer, I'm trying to come up with an _Integer() function that sets @error if the input is not valid. Such as a string returned from InputBox() but I'd also like it to handle numeric input. Since the new AutoIt3 release has changed Int() not to set @error I need to roll my own. Here's what I have. I'm just wondering if anyone sees a bug waiting to happen. $i = _Integer("57.7") If @error Then MsgBox(0x1010, "Test", "Int() returned error code: " & @error) Else MsgBox(0x1040, "Test", $i & " is a valid Int") EndIf Func _Integer($n) Local $legalChars = "-.0123456789" If IsString($n) Then For $x = 1 To StringLen($n) If Not StringInStr($legalChars, StringLeft($n, 1)) Then Return SetError(1, 0, 0) EndIf Next EndIf Return Int(Number($n)) EndFunc ;==>_Integer My Freeware Page Link to comment Share on other sites More sharing options...
ProgAndy Posted January 1, 2012 Share Posted January 1, 2012 It depends on what you want to allow as input. What about the string 2e4? Or 12apples? *GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes Link to comment Share on other sites More sharing options...
MilesAhead Posted January 1, 2012 Author Share Posted January 1, 2012 (edited) Basically whatever Int() did before the change is fine with me. Returning 0 with no error indicator kind of limits the usefulness of the function. So basically I'm trying to produce the same behavior as the old Int(). edit: I didn't remember this correctly. It's more like do $delayStr = InputBox() If $delayStr <> "" Then $delay = Int($delayStr) EndIf until Not @error If $delay then Yadda Yadda But now I need to call a filter function to make sure the string input is not valid since 0 is a valid delay(as in no delay.) Edited January 1, 2012 by MilesAhead My Freeware Page Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 1, 2012 Moderators Share Posted January 1, 2012 MilesAhead,Here are 3 possible solutions for you: - 1. Create your own input GUI and use the $ES_NUMBER style for the input control (upper input in first example below). But you do get the annoying Windows "error" bubble tip if you try to enter a non-digit.- 2. Create your own input GUI and do your own checking as the user enters data (lower input in this example).#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <WinAPI.au3> $hGUI = GUICreate("Integer only inputs", 320,120) $hInput_1 = GUICtrlCreateInput ( "", 10, 20, 300, 20, $ES_NUMBER) $hInput_2 = GUICtrlCreateInput ( "", 10, 70, 300, 20) GuiSetState(@SW_SHOW) ; Look for changes to inputs GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _WM_COMMAND($hWHnd, $iMsg, $wParam, $lParam) ; If it was an update message from our input If _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $hInput_2 Then ; Remove any non-digit characters GuiCtrlSetData($hInput_2, StringRegExpReplace(GUICtrlRead($hInput_2), "[^[:digit:]]", "")) EndIf EndFunc ;==>_WM_COMMAND- 3. If you really want to use InputBox, then you can check the returned value like this:HotKeySet("{ESC}", "On_Exit") While 1 ; Get user input $sData = InputBox("Delay", "Enter an integer value") ; Check for presence of non-digit characters If StringRegExp($sData, "[^[:digit:]]") Then MsgBox(0, "Error", "I said integers only, dummy!") Else MsgBox(0, "Success", "I see you read the instructions!") EndIf WEnd Func On_Exit() Exit EndFuncI hope one of those solves your problem. M23P.S. Updated to 3.3.8.0 yet? Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
jchd Posted January 1, 2012 Share Posted January 1, 2012 Hi Melba, 2) & 3) don't solve all, integers can be signed. HNY anyway! This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 1, 2012 Moderators Share Posted January 1, 2012 jchd,Smartarse! Bonne année à toi aussi. MilesAhead,I will see if I can get over this problem - but as you are looking for a "delay" value I doubt the user will enter a negative value! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ProgAndy Posted January 1, 2012 Share Posted January 1, 2012 This regular expression should cover all expressions which Int() recognizes as an integer:Func _Integer($i) If StringRegExp($i, "^s*[-+]?.?d") Then Return Int($i) Return SetError(1, 0, 0) EndFunc *GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes Link to comment Share on other sites More sharing options...
MilesAhead Posted January 1, 2012 Author Share Posted January 1, 2012 Thanks for the replies. Melba, you're right in this case the delay is going to be zero or greater, but it's still nice to have a general-purpose function. Thanks Andy for the regex. They're not my forte. The main thing that put me off Perl when I was playing with Linux was those substitution strings. Removing all the unexpected results from one of those was usually more work than the rest of the script. My Freeware Page 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