Jump to content

What is the difference between this coding?


ReconX
 Share

Recommended Posts

I've been watching tutorials and learning about AutoIt. I've just learned about how "Switch" & "Case" can be used. So, I implemented it into some coding I've been doing to test its functions, and it seems to function the same way from what I could tell. Are they basically the same thing, or are there advantages and disadvantages?

Case GUICtrlRead($hCombo)
                If GUICtrlRead($hCombo) = $aArray[0] Then
                    $fileDIR = GUICtrlRead($hDVD)
                    GUICtrlRead($FileList)
                    GUICtrlSetData($listview, $FileList)
                ElseIf GUICtrlRead($hCombo) = $aArray[1] Then
                    $fileDIR = GUICtrlRead($hBLU)
                    GUICtrlRead($FileList)
                    GUICtrlSetData($listview, $FileList)
                ElseIf GUICtrlRead($hCombo) = $aArray[2] Then
                    $fileDIR = GUICtrlRead($h4K)
                ElseIf GUICtrlRead($hCombo) = $aArray[3] Then
                    $fileDIR = GUICtrlRead($h3D)
                EndIf
Switch $hCombo
            Case GUICtrlRead($hCombo) = $aArray[0]
                $fileDIR = GUICtrlRead($hDVD)
                GUICtrlRead($FileList)
                GUICtrlSetData($listview, $FileList)
            Case GUICtrlRead($hCombo) = $aArray[1]
                $fileDIR = GUICtrlRead($hBLU)
                GUICtrlRead($FileList)
                GUICtrlSetData($listview, $FileList)
            Case GUICtrlRead($hCombo) = $aArray[2]
                $fileDIR = GUICtrlRead($h4K)
            Case GUICtrlRead($hCombo) = $aArray[3]
                $fileDIR = GUICtrlRead($h3D)
        EndSwitch

 

Link to comment
Share on other sites

use it like

Switch GUICtrlRead($hCombo)
            Case $aArray[0]
                $fileDIR = GUICtrlRead($hDVD)
                GUICtrlRead($FileList)
                GUICtrlSetData($listview, $FileList)
            Case $aArray[1]
                $fileDIR = GUICtrlRead($hBLU)
                GUICtrlRead($FileList)
                GUICtrlSetData($listview, $FileList)
            Case $aArray[2]
                $fileDIR = GUICtrlRead($h4K)
            Case $aArray[3]
                $fileDIR = GUICtrlRead($h3D)
EndSwitch

so you wouldn't have to call the GUICtrlRead() for every statement which means it should be faster. also it should be faster in general.

additional i would say it's more clearer to script with Switch instead of ElseIf especially when you have more statements to declare.

You can easily add an "OR" statement with just a "," like

Switch GUICtrlRead($hCombo)
            Case $aArray[0],$aArray[1]
                $fileDIR = GUICtrlRead($hDVD)
                GUICtrlRead($FileList)
                GUICtrlSetData($listview, $FileList)
            Case $aArray[2], $aArray[3]
                $fileDIR = GUICtrlRead($h4K)
EndSwitch

in this case you added here it wouldn't make sense at all, but in other cases it's faster to inject.

Edited by Aelc

why do i get garbage when i buy garbage bags? <_<

Link to comment
Share on other sites

In your first snippet, the Case is by itself only part of some control structure (Select or Switch), so it isn't a valid part of code by itself.

In your second snippet, you misinterpret the semantics of the control structure. I see that @Aelc just posted how to use Switch and what <expression> means in the help on Switch.

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)

Link to comment
Share on other sites

@ReconX They essentially perform the same task (if else logic vs. switch case), but I think Switch Case is optimized (more efficient processor time wise) compared to a lengthy if else chain.

@Aelc has a good point of not performing GUICtrlRead over and over.  It is just more efficient to call it once, store the result in a variable, then compare the variable.

@jchd Also has a point that as written, it wouldn't function, but I think you were just asking about the logic differences.

 

Edited by spudw2k
Link to comment
Share on other sites

3 minutes ago, spudw2k said:

@ReconX They essentially perform the same task (if else logic vs. switch case), but I think Switch Case is optimized (more efficient processor time wise) compared to a lengthy if else chain.

@Aelc has a good point of not performing GUICtrlRead over and over.  It is just more efficient to call it once, store the result in a variable, then compare the variable.

@jchd Also has a point that as written, it wouldn't function, but I think you were just asking about the logic differences.

 

I didn't post the full code as my coding is unorganized and messy right now as I am learning. But yes, I was asking about the logical differences between the two. 

Essentially, the code reads $hCombo and sets a primary directory depending on what is selected in $hCombo. If $hCombo is set to DVD ($sArray[0]), then set the user specified directory input by the user in a text field ($hDVD) as $fileDIR . 

Feel free to see the mess I've made. LMAO! I'm learning and cleaning up as I go. There are some things I need to work on. The buttons on the "Settings" tab disappear,  but reappear after clicking elsewhere. And I'm trying to get the listview to change when $hCombo changes to reflect the appropriate directory. I do want to note, that if you do test the coding, it does put a "\MovieInv\config.txt" in the temp folder. I just learned about Reading and Writing to an INI file, so that is coming next. :D

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GUIConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPI.au3>
#include <WinAPIFiles.au3>
#include <File.au3>
#include <WindowsConstants.au3>

; Here is the array
Global $aArray[4] = ["DVD", "Blu-ray", "4K", "3D"]
$userDesktop = EnvGet("USERPROFILE") & "\Desktop"
$userTMP = EnvGet("TMP") & "\MovieInv"
$config = $userTMP & "\config.txt"
$fileDIR = FileReadLine($config, 1)
$comboDefault = FileReadLine($config, 6)
$hTitle = "Add Movie to Inventory"
$iFileExists = FileExists($config)

; And here we get the elements into a list
$sList = ""
For $i = 0 To UBound($aArray) - 1
    $sList &= "|" & $aArray[$i]
Next

Example()

Func Example()

    $hGUI = GUICreate($hTitle, 460) ; will create a dialog box that when displayed is centered

    $hTab = GUICtrlCreateTab(10, 10, 441, 380)

    GUICtrlCreateTabItem("Inventory Movie")
    GUICtrlCreateGroup("", 20, 40, 420, 98) ;Start Group
    Opt("GUICoordMode", 0)
    $inputText1 = GUICtrlCreateLabel("Movie Name:", 10, 25, 75, 20)
    $inputName = GUICtrlCreateInput("", 90, -2, 289, 20)
    $tmdbSearch = GUICtrlCreateButton("+", 292, 0, 20, 20)
    $inputText2 = GUICtrlCreateLabel("Movie Year:", -382, 26, 75, 20)
    $inputYear = GUICtrlCreateInput("", 90, -2, 311, 20)
    $submit = GUICtrlCreateButton("Create", 110, 24, 201, 20)
    $openDIR = GUICtrlCreateButton("D", -24, -1, 20, 20)
    $hCombo = GUICtrlCreateCombo("", -85, -1, 80, 20, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
    GUICtrlSetData($hCombo, $sList, $comboDefault)

    ; _FileListToArray
    $FileList = _FileListToArrayRec($fileDIR, '*.mkv', $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT) ; Only files

;~  $listview = GUICtrlCreateListView("File Name|Year", -101, 40, 420, 190) ;,$LVS_SORTDESCENDING)
;~  For $n = 1 To UBound($FileList) - 1
;~      $File_Name = StringLeft($FileList[$n], StringInStr($FileList[$n], "(") - 2)
;~      $extractYear = StringMid($FileList[$n], StringInStr($FileList[$n], "(", 2, -1) + 1)
;~      $File_Year = StringMid($extractYear, 1, 4)
;~      $item = GUICtrlCreateListViewItem($File_Name & "|" & $File_Year, $listview)
;~      _GUICtrlListView_SetColumnWidth($listview, 0, 351)
;~  Next

    Local $listview = GUICtrlCreateListView("File Name|Year", -101, 40, 420, 190) ;,$LVS_SORTDESCENDING)
    For $i = 1 To UBound($FileList) - 1
        GUICtrlCreateListViewItem(_ArrayToString(StringRegExp($FileList[$i], "([^\\]*?)\h*\((\d+)\)", 1)), $listview)
    Next
    GUICtrlSendMsg($listview, $LVM_SETCOLUMNWIDTH, 0, -1)
    _GUICtrlListView_SetColumnWidth($listview, 0, 351)

    Opt("GUICoordMode", 1) ; GUI coords
    GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

    GUICtrlCreateTabItem("Settings")
    GUICtrlCreateGroup("Directories", 20, 40, 420, 135) ;Start Group
    Opt("GUICoordMode", 0)
    GUIStartGroup()
    $inputText1 = GUICtrlCreateLabel("DVD:", 10, 25, 75, 20)
    $inputText2 = GUICtrlCreateLabel("Blu-ray:", -1, 26, 75, 20)
    $inputText3 = GUICtrlCreateLabel("4K:", -1, 26, 75, 20)
    $inputText4 = GUICtrlCreateLabel("3D:", -1, 26, 75, 20)
    GUIStartGroup()
    $hDVD = GUICtrlCreateInput(FileReadLine($config, 2), 50, -80, 350, 20)
    $hBLU = GUICtrlCreateInput(FileReadLine($config, 3), -1, 26, 350, 20)
    $h4K = GUICtrlCreateInput(FileReadLine($config, 4), -1, 26, 350, 20)
    $h3D = GUICtrlCreateInput(FileReadLine($config, 5), -1, 26, 350, 20)
    GUIStartGroup()
    $bDVD = GUICtrlCreateButton("...", 331, -77, 18, 18)
    $bBLU = GUICtrlCreateButton("...", -1, 26, 18, 18)
    $b4K = GUICtrlCreateButton("...", -1, 26, 18, 18)
    $b3D = GUICtrlCreateButton("...", -1, 26, 18, 18)

    Opt("GUICoordMode", 1) ; GUI coords
    GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group
    GUICtrlSetState($hDVD, $GUI_DISABLE)
    GUICtrlSetState($hBLU, $GUI_DISABLE)
    GUICtrlSetState($h4K, $GUI_DISABLE)
    GUICtrlSetState($h3D, $GUI_DISABLE)

    GUICtrlCreateTabItem("") ; end tabitem definition

    GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $iFileExists
                If FileExists($config) Then
                    $userTMP = $userTMP
                Else
                    DirCreate($userTMP)
                    FileWrite($config, "")
                EndIf
            Case $GUI_EVENT_CLOSE
                _FileWriteToLine($config, 1, $fileDIR, True)
                _FileWriteToLine($config, 2, GUICtrlRead($hDVD), True, True)
                _FileWriteToLine($config, 3, GUICtrlRead($hBLU), True, True)
                _FileWriteToLine($config, 4, GUICtrlRead($h4K), True, True)
                _FileWriteToLine($config, 5, GUICtrlRead($h3D), True, True)
                _FileWriteToLine($config, 6, GUICtrlRead($hCombo), True, True)
                Exit
            Case $submit
                $movieName = GUICtrlRead($inputName)
                $movieYear = GUICtrlRead($inputYear)
                DirCreate($fileDIR)
                If FileExists($fileDIR & "\" & $movieName & " (" & $movieYear & ").mkv") Then
                    MsgBox($MB_SYSTEMMODAL, "Movie Exists!!!!", "This Movie Already Exists!!!!", 10)
                    GUICtrlSetData($inputName, "")
                    GUICtrlSetData($inputYear, "")
                    _WinAPI_SetFocus(ControlGetHandle("", "", $inputName))
                Else
                    FileWrite($fileDIR & "\" & $movieName & " (" & $movieYear & ").mkv", "PLACEHOLDER")
                    GUICtrlSetData($inputName, "")
                    GUICtrlSetData($inputYear, "")
                    _WinAPI_SetFocus(ControlGetHandle("", "", $inputName))
                EndIf
            Case $tmdbSearch
                $movieName = GUICtrlRead($inputName)
                ShellExecute("https://www.themoviedb.org/search/movie?query=" & $movieName)
                WinActivate($hGUI)
                _WinAPI_SetFocus(ControlGetHandle("", "", $inputYear))
            Case $bDVD
                Local $sDVDFolder = FileSelectFolder("Select a folder...", "%CSIDL_DRIVES%")
                If @error Then
                    MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
                Else
                    GUICtrlSetData($hDVD, $sDVDFolder)
                EndIf
            Case $bBLU
                Local $sBLUFolder = FileSelectFolder("Select a folder...", "%CSIDL_DRIVES%")
                If @error Then
                    MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
                Else
                    GUICtrlSetData($hBLU, $sBLUFolder)
                EndIf
            Case $b4K
                Local $s4KFolder = FileSelectFolder("Select a folder...", "%CSIDL_DRIVES%")
                If @error Then
                    MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
                Else
                    GUICtrlSetData($h4K, $s4KFolder)
                EndIf
            Case $b3D
                Local $s3DFolder = FileSelectFolder("Select a folder...", "%CSIDL_DRIVES%")
                If @error Then
                    MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
                Else
                    GUICtrlSetData($h3D, $s3DFolder)
                EndIf
            Case $openDIR
                ShellExecute($fileDIR)
                #comments-start
            Case GUICtrlRead($hCombo)
                If GUICtrlRead($hCombo) = $aArray[0] Then
                    $fileDIR = GUICtrlRead($hDVD)
                    GUICtrlRead($FileList)
                    GUICtrlSetData($listview, $FileList)
                ElseIf GUICtrlRead($hCombo) = $aArray[1] Then
                    $fileDIR = GUICtrlRead($hBLU)
                    GUICtrlRead($FileList)
                    GUICtrlSetData($listview, $FileList)
                ElseIf GUICtrlRead($hCombo) = $aArray[2] Then
                    $fileDIR = GUICtrlRead($h4K)
                ElseIf GUICtrlRead($hCombo) = $aArray[3] Then
                    $fileDIR = GUICtrlRead($h3D)
                EndIf
                #comments-end
        EndSwitch
        Switch $hCombo
            Case GUICtrlRead($hCombo) = $aArray[0]
                $fileDIR = GUICtrlRead($hDVD)
                GUICtrlRead($FileList)
                GUICtrlSetData($listview, $FileList)
            Case GUICtrlRead($hCombo) = $aArray[1]
                $fileDIR = GUICtrlRead($hBLU)
                GUICtrlRead($FileList)
                GUICtrlSetData($listview, $FileList)
            Case GUICtrlRead($hCombo) = $aArray[2]
                $fileDIR = GUICtrlRead($h4K)
            Case GUICtrlRead($hCombo) = $aArray[3]
                $fileDIR = GUICtrlRead($h3D)
        EndSwitch

    WEnd
EndFunc   ;==>Example

 

Link to comment
Share on other sites

Since you are going down the path of code optimization, may I offer another tip?

A good coding practice is to eliminate redundant code where feasible. A great opportunity which stands out looking at your code is the FileSelectFolder calls.

I would consider turning that into a function and calling the function instead.

Something like:

Case $bDVD
    _SetDirectoryFromFolderSelection($hDVD)
Case $bBLU
    _SetDirectoryFromFolderSelection($hBLU)
Case $b4K
    _SetDirectoryFromFolderSelection($h4K)
Case $b3D
    _SetDirectoryFromFolderSelection($h3D)
    
        
Func _SetDirectoryFromFolderSelection($hCtrl)
    Local $sFolder = FileSelectFolder("Select a folder...", "%CSIDL_DRIVES%")
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
    Else
        GUICtrlSetData($hCtrl, $sFolder)
    EndIf
EndFunc

 

Link to comment
Share on other sites

In addition to being less verbose than If/ElseIf, the Switch Case has the option of using a Continue Case statement, which can act as a logical waterfall, useful sometimes e.g. state machines.

From the example:

#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $sName = InputBox("", "Please enter a word.", "", " M", Default, Default, Default, Default, 10)
    Local $sMsg = ""

    Switch @error
        Case 2
            $sMsg = "Timeout "
            ContinueCase
        Case 1 ; Continuing previous case
            $sMsg &= "Cancellation"
        Case 0
            Switch $sName
                Case "a", "e", "i", "o", "u"
                    $sMsg = "Vowel"
                Case "QP"
                    $sMsg = "Mathematics"
                Case "Q" To "QZ"
                    $sMsg = "Science"
                Case Else
                    $sMsg = "Others"
            EndSwitch
        Case Else
            $sMsg = "Something went horribly wrong."
    EndSwitch

    MsgBox($MB_SYSTEMMODAL, "", $sMsg)
EndFunc   ;==>Example

 

Code hard, but don’t hard code...

Link to comment
Share on other sites

4 hours ago, spudw2k said:

Since you are going down the path of code optimization, may I offer another tip?

A good coding practice is to eliminate redundant code where feasible. A great opportunity which stands out looking at your code is the FileSelectFolder calls.

I would consider turning that into a function and calling the function instead.

Something like:

Case $bDVD
    _SetDirectoryFromFolderSelection($hDVD)
Case $bBLU
    _SetDirectoryFromFolderSelection($hBLU)
Case $b4K
    _SetDirectoryFromFolderSelection($h4K)
Case $b3D
    _SetDirectoryFromFolderSelection($h3D)
    
        
Func _SetDirectoryFromFolderSelection($hCtrl)
    Local $sFolder = FileSelectFolder("Select a folder...", "%CSIDL_DRIVES%")
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
    Else
        GUICtrlSetData($hCtrl, $sFolder)
    EndIf
EndFunc

 

That is a lot neater! Thanks for that tip. I think I understand it also. 

$bDVD, $bBLU, $b4K and $b3D are calling upon the function with; _SetDirectoryFromFolderSelection. $hCtrl represents one of the four strings being used and is being set by whatever is selected as a directory with $sFolder. So technically, if I were to be selecting a directory for $hDVD, the coding would be read as, "GUICtrlSetData($hDVD, $sFolder)". Do I have that somewhat correct?

 

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