Jump to content
Sign in to follow this  
MattX

Lots of ElseIfs

Recommended Posts

MattX

I'm changing a script of mine in which I want to have it ignore a long list of server names - [ around 50 of em ] - I can do this with lots of ElseIfs but was wondering if there was a better / more efficient of doing this. Even would be happy as naming them in one line instead of 50 lines.

Share this post


Link to post
Share on other sites
sleepydvdr

You could put the server names in a plain text file and run a loop reading each line of the file and performing your checks.

Or you could do one long one dimensional array and again, run the checks in a loop.


#include <ByteMe.au3>

Share this post


Link to post
Share on other sites
Melba23

MattX,

I would try putting all the names into a string and then using StringInStr to see if the specific server is in there: ;)

HotKeySet("{ESC}", "On_Exit")

$sList = "Server_1;Server_2;Server_3"

While 1

    $iRandom = Random(1, 5, 1)

    If StringInStr($sList, "Server_" & $iRandom) Then
        MsgBox(0, "Result", "Server_" & $iRandom & " is in the list")
    Else
        MsgBox(0, "Result", "Server_" & $iRandom & " is NOT in the list")
    EndIf

WEnd

Func On_Exit()
    Exit
EndFunc

I hope that helps. :)

M23

Edit: Just realised I needed to include the separator in the string to check, but I see Mat was quicker - youth wins again! ;)

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Mat

Melba, your method is flawed when a name is a substring of another.

I would make some slight amendments:

HotKeySet("{ESC}", "On_Exit")

$sList = ";Server_1;Server_2;Server_3;"

While 1

    $iRandom = Random(1, 5, 1)

    If StringInStr($sList, ";Server_" & $iRandom & ";") Then
        MsgBox(0, "Result", "Server_" & $iRandom & " is in the list")
    Else
        MsgBox(0, "Result", "Server_" & $iRandom & " is NOT in the list")
    EndIf

WEnd

Func On_Exit()
    Exit
EndFunc

Share this post


Link to post
Share on other sites
smartee

hi MattX,

As the Canadians of South Park say: "How aboot this?" :)

Local $sServerListString1 = "ServName1;ServName2;ServName3;ServName4"
Local $aServIgnoreList1 = StringSplit($sServerListString1, ";")

If _ServerSearch("servname1", $aServIgnoreList1) Then
    MsgBox(64, "", "Server is ignored")
Else
    MsgBox(64, "", "Server is NOT ignored")
EndIf

Func _ServerSearch($sServerName, ByRef $aServerList, $iCaseSense = -1)
    If $iCaseSense = -1 Then $iCaseSense = 0
    For $i = 1 To $aServerList[0]
        If StringCompare($aServerList[$i], $sServerName, $iCaseSense) = 0 Then Return True
    Next
    Return False
EndFunc   ;==>_ServerSearch

-smartee

Share this post


Link to post
Share on other sites
Mat

hi MattX,

As the Canadians of South Park say: "How aboot this?" :)

If you are using StringCompare then you might as well do a binary search as you can pre-sort the server names. My bet is that a string search is faster than a linear array search, and a binary search will probably be similar to stringInStr, maybe even slower.

#include<Array.au3>

HotKeySet("{ESC}", "On_Exit")

$sList = ""

For $i = 1 To 50
    $sList &= "Server_" & $i & ";"
Next

$aServerArray = StringSplit($sList, ";", 2)
$sServerList = ";" & $sList & ";"

_ArraySort($aServerArray)

; Lets do 3 lots of 10000 searches

Local $nAvr_str = 0, $nAvr_lin = 0, $nAvr_bin = 0

For $n = 1 To 3
    ; StringInStr (Melba)
    $iTimer = TimerInit()
    For $i = 1 To 10000
        $iRandom = Random(1, 100, 1)
        If StringInStr($sList, ";Server_" & $iRandom & ";") Then
            ;;;
        EndIf
    Next
    $nAvr_str += TimerDiff($iTimer)

    ; Linear search (smartee)
    $iTimer = TimerInit()
    For $i = 1 To 10000
        $iRandom = Random(1, 100, 1)
        If _ServerSearch("Server_" & $iRandom, $aServerArray) Then
            ;;;
        EndIf
    Next
    $nAvr_lin += TimerDiff($iTimer)

    ; Binary search
    $iTimer = TimerInit()
    For $i = 1 To 10000
        $iRandom = Random(1, 100, 1)
        If _ArrayBinarySearch($aServerArray, "Server_" & $iRandom) Then
            ;;;
        EndIf
    Next
    $nAvr_bin += TimerDiff($iTimer)
Next

$nAvr_str /= 30000
$nAvr_lin /= 30000
$nAvr_bin /= 30000

ConsoleWrite(StringFormat("Str: %.2f\nLin: %.2f\nBin: %.2f\n", $nAvr_str, $nAvr_lin, $nAvr_bin))

Func _ServerSearch($sServerName, ByRef $aServerList, $iCaseSense = -1)
    If $iCaseSense = -1 Then $iCaseSense = 0
    For $i = 0 To UBound($aServerList) - 1
        If StringCompare($aServerList[$i], $sServerName, $iCaseSense) = 0 Then Return True
    Next
    Return False
EndFunc   ;==>_ServerSearch

Func On_Exit()
    Exit
EndFunc   ;==>On_Exit

Share this post


Link to post
Share on other sites
jchd

Guys, faster or slower search of 1 in 50 short strings occasionally (sever name) is probably not going to mak any noticeable difference.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
smartee

If you are using StringCompare then you might as well do a binary search as you can pre-sort the server names.

If you are considering the case where the array is sufficiently large so as to warrant a binary search, then why format the list as a string, only to split it later? Just place it into the array during declaration like this:
Local $aServerArray[4] = [3, "Server_Uno", "Server_Dos", "_Server_Three"]

-smartee

Share this post


Link to post
Share on other sites
smartee

Guys, faster or slower search of 1 in 50 short strings occasionally (sever name) is probably not going to mak any noticeable difference.

That's what I was thinking; but but but Mat started it :)

Share this post


Link to post
Share on other sites
JohnOne

Or use an ini file

$aServers = IniReadSection("testserver.ini","SERVERS")

For $i = 1 To $aServers[0][0]
    If $aServers[$i][0] = "server" & String($i) Then
        ConsoleWrite($aServers[$i][0] & " exists" & @CRLF)
    EndIf
Next

[SERVERS]
server1=
server2=
server3=

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

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
Mat

If you are considering the case where the array is sufficiently large so as to warrant a binary search, then why format the list as a string, only to split it later? Just place it into the array during declaration like this:

Local $aServerArray[4] = [3, "Server_Uno", "Server_Dos", "_Server_Three"]

Because in that case I needed a string as well. Besides, I found that using StringInStr was always faster anyway, and since it's easier to the user, I'd say it's the bast way.

The only reason I mentioned speed is because Smartee was using a linear search on a list that could easily be sorted, so binary search is the right tool for the job. Who knows? Maybe it's the bottleneck in a tight loop somewhere in the OP's code.

Share this post


Link to post
Share on other sites
MattX

I've done the lazy part [ just to get it working first ] and added around 50 ElseIfs [ Cut and paste is your friend !! ] but I will most probably go down the .ini file route and get the script to read that in first - I can then easily add any new server to that.

Many thanks for all the suggestions - I'm now looking for a way of getting a user's actual exchange E-mail address somehow from their logon name......

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.