tes5884 Posted December 18, 2013 Posted December 18, 2013 Is there a way to create an IF loop comparing multiple values in a single statement? Something like below. IF $x <> "1" OR "2" OR "3" Then ConsoleWrite("Success") EndIf Thanks! www.tspitz.com
Moderators Solution Melba23 Posted December 18, 2013 Moderators Solution Posted December 18, 2013 tes5884,Use Switch:$x = 6 Switch $x Case 1, 2, 3 ConsoleWrite("Fail" & @CRLF) Case Else ConsoleWrite("Success" & @CRLF) EndSwitchAll clear? M23 tes5884 1 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
tes5884 Posted December 18, 2013 Author Posted December 18, 2013 tes5884, Use Switch: $x = 6 Switch $x Case 1, 2, 3 ConsoleWrite("Fail" & @CRLF) Case Else ConsoleWrite("Success" & @CRLF) EndSwitch All clear? M23 Thanks! www.tspitz.com
MilesAhead Posted December 18, 2013 Posted December 18, 2013 (edited) You can do it using If like IF $x <> "1" OR $x <> "2" OR $x <> "3" Then ConsoleWrite("Success") On single line If you don't need EndIf Also see _Iif() you can nest them inside each other. But for a lot of disparate values switch,as suggested, is better. Edit: of course in the If example, using And makes more sense.. as in $x does not equal any of the values listed, rather than using Or. Edited December 18, 2013 by MilesAhead My Freeware Page
LoWang Posted December 30, 2018 Posted December 30, 2018 Can't I write something like if($mylongvariablenameOrExpression in ("h","m","s")) then something ?
Moderators JLogan3o13 Posted December 30, 2018 Moderators Posted December 30, 2018 @LoWang did you try it? "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
LoWang Posted December 30, 2018 Posted December 30, 2018 (edited) 1 minute ago, JLogan3o13 said: @LoWang did you try it? I tried and failed obviously. Why would I ask if it worked? ;-) There is a possibility I just use wrong syntax and there is a way to write it similar to this and that is why I ask Edited December 30, 2018 by LoWang
Moderators JLogan3o13 Posted December 30, 2018 Moderators Posted December 30, 2018 1 minute ago, LoWang said: Why would I ask if it worked? ;-) That is a good question; most wouldn't ask if they'd already answered their own question. The best practice way of doing this (as was pointed out just about exactly 5 years ago) is to use a Switch. "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
LoWang Posted December 30, 2018 Posted December 30, 2018 I just wanted to make the code more compact because using switch would generate many lines. Some other languages allow a coder to use similar syntax, so I wondered if AutoIt knows it too. But maybe I found a compact way like this: #include <Array.au3> local $possibleChars[]=["h","m","s"] $k=_ArraySearch($possibleChars,$myChar) ;$k is not needed but must be there if @error then msgbox(16,"ERROR","invalid char") BTW I know this thread is old but I don't see a reason to fragment topics because some things don't change
Nine Posted December 30, 2018 Posted December 30, 2018 I remember many years ago where number of lines in a program was important because each line was occupying memory space and memory space costed M$. Today only thing left is efficiency. How fast can you do a task. So instead of asking what is the least number of lines you can do something, you should verify how fast is one compare to the other. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
LoWang Posted December 30, 2018 Posted December 30, 2018 2 minutes ago, Nine said: I remember many years ago where number of lines in a program was important because each line was occupying memory space and memory space costed M$. Today only thing left is efficiency. How fast can you do a task. So instead of asking what is the least number of lines you can do something, you should verify how fast is one compare to the other. OK, but I believe readability of a code is also important. If a relatively simple function is so long that it does not fit on a screen then it's harder to understand it (load it into a brain memory) and work on it especially if I revive old code or somebody else starts working on it. Anyway I think both my question and example are clear, so there is no need to challenge it's validity ;-)
Moderators JLogan3o13 Posted December 30, 2018 Moderators Posted December 30, 2018 @LoWang you saved yourself 3 whole lines from doing it the way Melba suggested above, but in so doing you had to introduce an entire UDF. To each their own, but I doubt most people would count that as a big win "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
LoWang Posted December 30, 2018 Posted December 30, 2018 (edited) ok that switch with comma separated values would be similar actually I see Edited December 30, 2018 by LoWang
mikell Posted January 1, 2019 Posted January 1, 2019 On 31/12/2018 at 12:10 AM, LoWang said: Anyway I think both my question and example are clear, Not really... if($mylongvariablenameOrExpression in ("h","m","s")) then something Does this mean : if one of these chars "h", "m", "s" is present in $mylongvariablenameOrExpression then something ? It's a quite different situation
Gianni Posted January 2, 2019 Posted January 2, 2019 .... with a little help from SQLite #include <SQLite.au3> ; -- turn on the sql engine -------------------- Global Static $g__sSQliteDll = _SQLite_Startup() ; -- open a 'memory' DB -- Global Static $hDb = _SQLite_Open() ; ---------------------------------------------- If _IsInList(1, '2,3,4,5,6,7,8,9') Then MsgBox(0, '', 'there is') Else MsgBox(0, '', 'there is not') EndIf Func _IsInList($Target, $List) Local $aMyResult, $iMyRows, $iMyColumns _SQLite_GetTable(-1, "SELECT " & $Target & " IN (" & $List & ");", $aMyResult, $iMyRows, $iMyColumns) Return 1 = $aMyResult[UBound($aMyResult) - 1] EndFunc ;==>_IsInList _SQLite_Shutdown() p.s. you have to save sqlite3.dll along with your script. take it from here if you do not have it yet -> https://www.sqlite.org/download.html (Precompiled Binaries for Windows session) Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
Moderators JLogan3o13 Posted January 2, 2019 Moderators Posted January 2, 2019 My question is the same; there are 1000 ways to skin the proverbial cat in AutoIt, we all know this. And everyone loves to demonstrate their own take on the different ways things can be done. But how is this approach better than the straightforward way presented by Melba several years ago, and how does it satisfy the user request of making the code more compact? "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
Gianni Posted January 2, 2019 Posted January 2, 2019 (edited) nothing against other ways of doing things and not necessarily a better way, I simply love try/see/run across new techniques and approaches. looking for and trying new ways of doing old things sometime can open new perspectives, new stimuli ..... sometime not. Edited January 2, 2019 by Chimp Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
bailey1274 Posted July 2, 2019 Posted July 2, 2019 This may be an older post but this is usually how I go upon really long if not statements. Seems pretty compact $Animal = 'duck' If StringInStr('cat,dog,fish',$Animal) = False Then Msgbox(0,'animal','Quack') EndIf ahmeddzcom 1
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