Sign in to follow this  
Followers 0
MilesAhead

_Integer() sets @error

8 posts in this topic

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

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 by MilesAhead

Share this post


Link to post
Share on other sites

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
EndFunc

I hope one of those solves your problem. ;)

M23

P.S. Updated to 3.3.8.0 yet? :D


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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)

Share this post


Link to post
Share on other sites

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! :D

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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. :)

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