Jump to content

Easy way to write "If... Else... " with multiple strings?


Recommended Posts

Wondering what a more efficient way to tackle this?

I have been previously been doing: If $string = $string[1] And $string = $string[2] And etc...

$string = StringSplit("cat|dog|bird|mouse|snake|horse|cow|pig|chicken", "|")

If $string = (all of the above strings) Then
    MsgBox(0, "", "Farm Animals")
Else
    MsgBox(0,"", "Nothing")
EndIf
Link to comment
Share on other sites

Wondering what a more efficient way to tackle this?

I have been previously been doing: If $string = $string[1] And $string = $string[2] And etc...

$string = StringSplit("cat|dog|bird|mouse|snake|horse|cow|pig|chicken", "|")

If $string = (all of the above strings) Then
    MsgBox(0, "", "Farm Animals")
Else
    MsgBox(0,"", "Nothing")
EndIf
Use EXP pattern or use basic if..else script with AND or use For loop :D

for(loop=0; loop<infinity; loop++) { alert('I love you'); }

Link to comment
Share on other sites

Wondering what a more efficient way to tackle this?

I have been previously been doing: If $string = $string[1] And $string = $string[2] And etc...

Use a For Next loop:

$string = StringSplit("cat|dog|bird|mouse|snake|horse|cow|pig|chicken", "|")
$input = InputBox("Farm Animals", "What farm animal?")
For $i = 0 to Ubound($string) - 1
 If($input == $string[i]) Then
  MsgBox(0, "", "Farm Animals")
 Else
  MsgBox(0, "", "Nothing")
 EndIf
Next
Link to comment
Share on other sites

:D

Damn C++, I've wrapped the expression in parenthesis.

I like your energy but won't this provide the message box the amount of times that is included in the string which if the string was to be 50+ entries would become quite annoying?

Link to comment
Share on other sites

You're not explaining yourself very well.

I never do... Here is a better scenario which I hope explains better!?

Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)

#Include <GUIConstantsEx.au3>

Global $data = "cat|dog|bird|mouse|snake|horse|cow|pig|chicken";currently 9 entries - update 'Func _button()' if adding more...
Global $string = StringSplit($data, "|") 
Global $combo

GUICreate("", 300, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "_events")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "_events")
$combo = GUICtrlCreateCombo("", 20, 20, 260, 20)
GUICtrlSetData($combo, $data, "|")
GUICtrlCreateButton("check", 130, 50, 50, 20)
GUICtrlSetOnEvent(-1, "_button")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _button()
    If GUICtrlRead($combo) = $string[1] Or GUICtrlRead($combo) = $string[2] Or GUICtrlRead($combo) = $string[3] Or GUICtrlRead($combo) = $string[4] Or GUICtrlRead($combo) = $string[5] Or GUICtrlRead($combo) = $string[6] Or GUICtrlRead($combo) = $string[7] Or GUICtrlRead($combo) = $string[8] Or GUICtrlRead($combo) = $string[9] Then
        MsgBox(0, "", "Farm Animals")
    Else
        MsgBox(0,"", "Nothing")
    EndIf
EndFunc;==> Button

Func _events()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            Exit
        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
            GUISetState(@SW_MINIMIZE)
    EndSelect
EndFunc;==> Events
Link to comment
Share on other sites

Done.

Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)

#include <GUIConstantsEx.au3>

Global $data = "cat|dog|bird|mouse|snake|horse|cow|pig|chicken";currently 9 entries - update 'Func _button()' if adding more...
Global $string = StringSplit($data, "|")
Global $combo

GUICreate("", 300, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "_events")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "_events")
$combo = GUICtrlCreateCombo("", 20, 20, 260, 20)
GUICtrlSetData($combo, $data, "|")
GUICtrlCreateButton("check", 130, 50, 50, 20)
GUICtrlSetOnEvent(-1, "_button")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _button()
    For $i = 0 To UBound($string) - 1
        If GuiCtrlRead($combo) = $string[$i] Then
            MsgBox(0, "", "Farm Animals")
            ExitLoop
        Else
            ContinueLoop
        EndIf
    Next
EndFunc  ;==>_button

Func _events()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            Exit
        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
            GUISetState(@SW_MINIMIZE)
    EndSelect
EndFunc  ;==>_events

Have fun.

Link to comment
Share on other sites

yeah the FOR loop was the obvious solution i was just wondering if there was another way to condense the IF statement without running a loop.

Wondering why you are using "UBound($string) - 1" instead of "$string[0]" as this provides the total entries within the array?

I am still learning and just wondering if there was an advantage of using one over the other?

Link to comment
Share on other sites

Wondering why you are using "UBound($string) - 1" instead of "$string[0]" as this provides the total entries within the array?

$string[0] provides the total entries within the array only, if set with "UBound($string) - 1" within the UDF (or by other means, e.g. a counter), stringsplit is on of those func's that do, but $sting[0] can also be the first entry of the array.
Link to comment
Share on other sites

Here are two ways of doing it.

;
Local $string = StringSplit("cat|dog|bird|mouse|snake|horse|cow|pig|chicken", "|")
Local $res

Do
    $res = "Nothing"
    Local $input = InputBox("Farm Animals", "What farm animal?")
    If @error = 1 Then Exit
    For $i = 0 To UBound($string) - 1
        If ($input == $string[$i]) Then $res = "Farm Animals"
    Next
    MsgBox(0, "", $res)
Until Not Sleep(10)
;
or

;
Local $string = "cat|dog|bird|mouse|snake|horse|cow|pig|chicken"
Local $res

Do
    $res = "Nothing"
    Local $input = InputBox("Farm Animals", "What farm animal?")
    If @error = 1 Then Exit

    If StringInStr($string, $input) <> 0 Then $res = "Farm Animals"
    MsgBox(0, "", $res)
    
Until Not Sleep(10)
;
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...