Jump to content

Making sure string is a number


nitekram
 Share

Recommended Posts

I know it has been asked before, probably by me, but I can seem to located it.

How do I make sure Input() is a number and that number is between 0 and 24?

Do
    $iTimeToConvert = InputBox('Enter the time in military time', 'Please enter the time in military time (0-24)! ' & $iCount)
    ;$iTimeToConvert = Int($iTimeToConvert)
    ;MsgBox('','$iTimeToConvert',$iTimeToConvert)
    If @error Then
        $iTimeToConvert = "-666"
    Else

        If $iTimeToConvert >= '0' Or $iTimeToConvert <= '24' Then
            $iCount += 1
        $bTrue = True

    EndIf
    EndIf
Until $iTimeToConvert = "-666" Or $bTrue = True

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Try this:

Do
    $iTimeToConvert = String(InputBox('Enter the time in military time', 'Please enter the time in military time (0-24)! '))
    If $iTimeToConvert = "" Then ExitLoop ;cancel was pressed
    Switch Int($iTimeToConvert)
        Case 0
            If Not StringRegExpReplace($iTimeToConvert, "[0]", "") Then
                ConsoleWrite("0 was entered" & @LF)
                ExitLoop ; "0" was entered
            EndIf
        Case 1 to 24 ; "1" to "24" was entered
            ConsoleWrite(Int($iTimeToConvert) & " was entered" & @LF)
            ExitLoop
    EndSwitch
Until False
Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Try this:

$iCount = 0
Do
    $iTimeToConvert = String(InputBox('Enter the time in military time', 'Please enter the time in military time (0-24)! ' & $iCount, ))
    Switch $iTimeToConvert
        Case "0" To "24"
            ExitLoop
    EndSwitch
Until False

Br,

UEZ

 

OK, one down, one to go...I still need it to close if you were to cancel the input box?

Do
    $iTimeToConvert = String(InputBox('Enter the time in military time', 'Please enter the time in military time (0-24)! ' & $iCount))
    If $iTimeToConvert = @error Then
        If @error = 1  Or $iTimeToConvert = 1 Then
    $iTimeToConvert = "-666"
    ExitLoop
    EndIf
    EndIf
    Switch $iTimeToConvert
        Case "0" To "24"
            ExitLoop
    EndSwitch
Until False

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Thanks for your help...but I need 0 (zero) as that is midnight in military time - this is being used as a time zone converter, so zero hours happen all the time.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

"0" should work as expected.

Updated the example again.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Try this:

Do
    $iTimeToConvert = String(InputBox('Enter the time in military time', 'Please enter the time in military time (0-24)! '))
    Switch Int($iTimeToConvert)
        Case 0
            If Not StringRegExpReplace($iTimeToConvert, "[0]", "") Then
                ConsoleWrite("0 was entered" & @LF)
                ExitLoop ; "0" was entered
            EndIf
        Case 1 to 24 ; "1" to "24" was entered
            ConsoleWrite(Int($iTimeToConvert) & " was entered" & @LF)
            ExitLoop
    EndSwitch
Until False Or $iTimeToConvert = "" ;cancel was pressed
Br,

UEZ

 

This will work...thanks again. You would think there would already been some function written for this, but all the ones that I tried would set a letter as a 0 (zero), but that would not work.

Thanks for your time on this.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Hmm, I think I thought in a wrong direction.

Can you provide me some valid examples please?

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Just a note, remember that 24 hours format is 0-23 and not 0-24 ;-)

Oh you are so right - lol

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Hmm, I think I thought in a wrong direction.

Can you provide me some valid examples please?

Br,

UEZ

What you did works...I tried to use the Number() and when you entered a char it shot back a zero, which was valid. I do not know how to use RexEx yet - that should be on my list of things to learn, but doing other things at the moment.

I also used Int() and that did not work either.

EDIT $hit, I forgot to leave a return after my code. So I have to enter my edit here...I cannot get into that corner to make a carriage return.

Anyway, using these two examples - if you enter an 'A' or 'a' or any letter for that matter, it will get you out of the loop.

Do
    $iTimeToConvert = InputBox('Enter the time in military time', 'Please enter the time in military time (0-24)! ')
    If @error then MsgBox('','','error = ' & @error)
    $iTimeToConvert = Number($iTimeToConvert)
Until $iTimeToConvert >= 0 And $iTimeToConvert <= 23

Do
    $iTimeToConvert = InputBox('Enter the time in military time', 'Please enter the time in military time (0-24)! ')
    If @error then MsgBox('','','error = ' & @error)
    $iTimeToConvert = Int($iTimeToConvert)
Until $iTimeToConvert >= 0 And $iTimeToConvert <= 23
Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Following the script on your first post...

Global $iCount, $bTrue = False
Do
    $iTimeToConvert = InputBox('Enter the time in military time', 'Please enter the time in military time (0-23)! ' & $iCount)
    If @error Then
        $iTimeToConvert = "-666"
    Else
        If StringRegExp($iTimeToConvert, "^([0-9]|1?[0-9]|2[0-3])$") Then
            $bTrue = True
        Else
            $iCount += 1
        EndIf
    EndIf
Until $iTimeToConvert = "-666" Or $bTrue = True

That should work for numbers between 0 and 23.

Edit: Typo ;)

Cheers,

sahsanu

Edited by sahsanu
Link to comment
Share on other sites

What about this?
 

Do
    $iTimeToConvert = InputBox('Enter the time in military time', 'Please enter the time in military time (0-23)! ')
    If @error then MsgBox('','','error = ' & @error)
Until Int(String($iTimeToConvert)) > -1 And Int(String($iTimeToConvert)) < 24 And (Not StringRegExpReplace(String($iTimeToConvert), "[\d+]", ""))

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

I still think that there should be a way of just asking if what ever is in the variable is a number - even if it is a string, the string is a number, meaning an 'a' is not a 2? Maybe you need to convert it to Chr() first and then find out what the value is?

Does anyone have any other ideas with out using RegEx commands?

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

You could always make your own.
Something like...

#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>

Do
    $iTimeToConvert = _InputBox('Enter the time in military time', 'Please enter the time in military time (0-24)! ')
    If @error Then MsgBox('', '', 'error = ' & @error)
    $iTimeToConvert = Int($iTimeToConvert)
Until $iTimeToConvert >= 0 And $iTimeToConvert <= 23

Func _InputBox($Title, $Prompt)
    $hGui = GUICreate($Title, 259, 190, Default, Default, $WS_THICKFRAME)
    GUICtrlCreateLabel($Prompt, 5, 10, 249, Default, $SS_CENTER)
    $hInput = GUICtrlCreateInput("", 10, 96, 223, 20, $ES_NUMBER)
    $OK = GUICtrlCreateButton("Ok", 28, 121, 75, 23)
    $Cancel = GUICtrlCreateButton("Cancel", 140, 121, 75, 23)
    GUISetState()
    Do
        Switch GUIGetMsg()
            Case -3
                GUIDelete($hGui)
                ExitLoop
            Case $OK
                $rtn = Int(GUICtrlRead($hInput))
                If $rtn = "" Then Return -1
                GUIDelete($hGui)
                Return $rtn
            Case $Cancel
                GUIDelete($hGui)
                ExitLoop
        EndSwitch
    Until 0
EndFunc   ;==>_InputBox
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I still think that there should be a way of just asking if what ever is in the variable is a number - even if it is a string, the string is a number, meaning an 'a' is not a 2? Maybe you need to convert it to Chr() first and then find out what the value is?

Does anyone have any other ideas with out using RegEx commands?

 

look at this

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

That will work...I knew that it had to be around.

EDIT oh, and thanks!!!

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...