leuce Posted December 3, 2016 Share Posted December 3, 2016 (edited) Hello everyone In this script: $selectoption = InputBox ("Select option", "Type 1, 2, 4 or 4", "5") If NOT $selectoption = "1" Then If NOT $selectoption = "2" Then If NOT $selectoption = "3" Then If NOT $selectoption = "4" Then MsgBox (0, "Can't read option", "Option should be 1, 2, 3 or 4", 0) Exit EndIf EndIf EndIf EndIf MsgBox (0, "", "Grrr, it didn't work!", 0) I expect the script to exit at the first MsgBox, if the $selectoption is anything other than 1, 2, 3 or 4 (e.g. if it is "5" or "hello"). But no, the script gives me the second MsgBox. What am I doing wrong? Thanks Samuel Edited December 3, 2016 by leuce Link to comment Share on other sites More sharing options...
j0kky Posted December 3, 2016 Share Posted December 3, 2016 (edited) you lost parentesis, the correct syntax is: If Not (variable1 = variable2) Then Instead, if you write: If Not variable1 = variable2 Then is equal to: If (Not variable1) = variable2 Then Edited December 3, 2016 by j0kky Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
leuce Posted December 3, 2016 Author Share Posted December 3, 2016 Thanks, I never knew that I had to use brackts. It doesn't say so in the help files. :-( Link to comment Share on other sites More sharing options...
j0kky Posted December 3, 2016 Share Posted December 3, 2016 It's because that's an implicit logical operator rule, just like +-*/ priority in math Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
mikell Posted December 3, 2016 Share Posted December 3, 2016 Alternative for the nested IFs $selectoption = InputBox ("Select option", "Type 1, 2, 3 or 4", "5") If NOT StringRegExp($selectoption, '^[1234]$') Then _ Exit MsgBox (0, "Can't read option", "Option should be 1, 2, 3 or 4", 0) MsgBox (0, "", "it worked !", 0) Link to comment Share on other sites More sharing options...
Developers Jos Posted December 3, 2016 Developers Share Posted December 3, 2016 1 hour ago, leuce said: Thanks, I never knew that I had to use brackts. It doesn't say so in the help files. :-( Are you sure about that? Strait from the helpfile: Quote Remarks This version of the If statement is used to execute a single statement without the overhead of an EndIf.The expression can contain the boolean operators of And, Or, and Not as well as the logical operators <, <=, >, >=, =, ==, and <> grouped with parentheses as needed. Related Jos 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...
water Posted December 3, 2016 Share Posted December 3, 2016 (edited) Why do you use an invalid value as default for the InputBox? $selectoption = Number(InputBox ("Select option", "Type 1, 2, 3 or 4")) If $selectoption < 1 Or $selectoption > 4 Then MsgBox (0, "Invalid selection", "Option should be 1, 2, 3 or 4") Edited December 3, 2016 by water My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
iamtheky Posted December 3, 2016 Share Posted December 3, 2016 (edited) number like that leaves edge cases for days because of the way it behaves with mixed strings, I would go very literal if you only have 4 options $selectoption = number(InputBox ("Select option", "Type 1, 2, 3 or 4" , "3hello")) If $selectoption < 1 Or $selectoption > 4 Then MsgBox (0, "Invalid selection", "Option should be 1, 2, 3 or 4") $selectoption = InputBox ("Select option", "Type 1, 2, 3 or 4" , "3hello") If stringregexp($selectoption , "1/z|2/z|3/z|4/z") = 0 Then MsgBox (0, "Invalid selection", "Option should be 1, 2, 3 or 4") Edited December 3, 2016 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
water Posted December 3, 2016 Share Posted December 3, 2016 (edited) Enhanced version $sSelectOption = InputBox("Select option", "Type 1, 2, 3 or 4") $iSelectOption = Number($sSelectOption) If StringLen($sSelectOption) <> 1 Or ($iSelectOption < 1 Or $iSelectOption > 4) Then MsgBox(0, "Invalid selection", "Option should be 1, 2, 3 or 4") Edited December 3, 2016 by water My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
mikell Posted December 3, 2016 Share Posted December 3, 2016 iamtheky, Your regex example doesn't work for me Looks like a typo, but if you meant 1\z etc then it allows 51 so it's less precise than '^[1234]$' Link to comment Share on other sites More sharing options...
iamtheky Posted December 3, 2016 Share Posted December 3, 2016 "Pointing out other people's problems with something that has lots of problems itself". Is my new slogan. mikell 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
mikell Posted December 3, 2016 Share Posted December 3, 2016 Did you say 'problems' ? Quote Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. kylomas, j0kky and iamtheky 3 Link to comment Share on other sites More sharing options...
kylomas Posted December 4, 2016 Share Posted December 4, 2016 mikell, You made my night! Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
leuce Posted December 4, 2016 Author Share Posted December 4, 2016 19 hours ago, Jos said: Are you sure about that? Strait from the helpfile: Jos Missed that, sorry. Link to comment Share on other sites More sharing options...
leuce Posted December 4, 2016 Author Share Posted December 4, 2016 19 hours ago, water said: Why do you use an invalid value as default for the InputBox? $selectoption = Number(InputBox ("Select option", "Type 1, 2, 3 or 4")) If $selectoption < 1 Or $selectoption > 4 Then MsgBox (0, "Invalid selection", "Option should be 1, 2, 3 or 4") For testing purposes. The final script will have a valid default value. Link to comment Share on other sites More sharing options...
Exit Posted December 4, 2016 Share Posted December 4, 2016 my 2 cents $selectoption = Number(InputBox ("Select option", "Type 1, 2, 3 or 4",""," M1")) If Not StringInStr("1234",$selectoption) Then MsgBox ("", "Invalid selection", "Option should be 1, 2, 3 or 4") App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
iamtheky Posted December 4, 2016 Share Posted December 4, 2016 combobox would prevent all the additional fun we can have making inputbox do tricks. ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) 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