Jump to content

IF <> Multiple values??


tes5884
 Share

Go to solution Solved by Melba23,

Recommended Posts

  • Moderators
  • Solution

tes5884,

Use Switch:

$x = 6

Switch $x
    Case 1, 2, 3
        ConsoleWrite("Fail" & @CRLF)
    Case Else
        ConsoleWrite("Success" & @CRLF)
EndSwitch
All clear? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

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 by MilesAhead
Link to comment
Share on other sites

  • 5 years later...
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 by LoWang
Link to comment
Share on other sites

  • Moderators

 

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ;-)

Link to comment
Share on other sites

  • Moderators

@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!

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

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

  • Moderators

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!

Link to comment
Share on other sites

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 by Chimp

 

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

  • 5 months later...

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

×
×
  • Create New...