Jump to content

More elegant/effective way to use Select/Case


stealth
 Share

Recommended Posts

Hi! I want to improve my coding by using Select/Case instead of several If/ElseIf statements.

Depending on the value of $CRTN and $PKG, I want to return the following:

1 LOOSE CARTON

2 LOOSE CARTONS

1 CARTON ON 1 SKID

3 CARTONS ON 1 SKID

123 CARTONS ON 5 SKIDS

By CARTON(S), I mean box(es).By SKID(S), I mean pallet(s).

Both of the following are impossible solutions for my purpose, minding the English singular and plural:

"x LOOSE CARTON(S) ON y SKID(S)" If carton(s) are loose, then they are not on a skid(s).

"1 CARTON on >1 SKID" 1 carton does not fit on multiple skids.

"5 CARTONS on 4 SKIDS" the number of skids must be =>the number of cartons

In practice, $CRTN and $PKG refer to Excel (with error trapping for opening the Excel Application):

$CRTN=$oExcel.Activesheet.Cells(1,1).Value

$PKG=$oExcel.Activesheet.Cells(1,2).Value

But in theory, I just define the dims in SciTE4 editor, change the values each time for confirmation it does what I'm telling it to. Notepad must be open.

$Loose=MsgBox(4,"Loose?","Are the cartons on a skid?")
$CRTN=1
$PKG=1

;---If/ElseIf---------
WinActivate("Untitled - Notepad")
If $Loose=6 Then
If $CRTN=1 Then
Send($CRTN & " LOOSE CARTON")
ElseIf $CRTN>1 Then
Send($CRTN & " LOOSE CARTONS")
EndIf
EndIf

If $Loose=7 Then
If $CRTN=1 Then
If $PKG=1 Then
Send($CRTN&" CARTON ON "&$PKG&" SKID")
EndIf
EndIf

If $CRTN>1 Then
If $PKG=1 Then
Send($CRTN&" CARTONS ON "&$PKG&" SKID")
ElseIf $PKG>1 Then
Send($CRTN&" CARTONS ON "&$PKG&" SKIDS")
EndIf
EndIf
EndIf

Then I rewrote the mess above using Select and Case

;----with Select/Case----
$Loose=MsgBox(4,"Loose?","Are the cartons on a skid?")
$CRTN=1
$PKG=1

WinActivate("Untitled - Notepad")
Select
Case $Loose = 7 AND $CRTN=1
Send($CRTN & " LOOSE CARTON")
Case $Loose =7 AND $CRTN>1
Send($CRTN & " LOOSE CARTONS")
EndSelect

WinActivate("Untitled - Notepad")
Select
Case $Loose = 6 AND $CRTN=1 AND $PKG=1
Send($CRTN&" CARTON ON "&$PKG&" SKID")
Case $Loose =6 AND $CRTN >1 AND $PKG=1
Send($CRTN&" CARTONS ON "&$PKG&" SKID")
Case $Loose =6 AND $CRTN >1 AND $PKG>1
Send($CRTN&" CARTONS ON "&$PKG&" SKIDS")
EndSelect

Then I rewrote Select and case using only 1 argument.

;----with Select/Case but better----
$Loose=MsgBox(4,"Loose?","Are the cartons on a skid?")
$CRTN=1
$PKG=1
WinActivate("Untitled - Notepad")
Select
Case $Loose = 7 AND $CRTN = 1
Send($CRTN & " LOOSE CARTON")
Case $Loose = 7 AND $CRTN > 1
Send($CRTN & " LOOSE CARTONS")
Case $Loose = 6 AND $CRTN = 1 AND $PKG = 1
Send($CRTN&" CARTON ON "&$PKG&" SKID")
Case $Loose = 6 AND $CRTN > 1 AND $PKG = 1
Send($CRTN&" CARTONS ON "&$PKG&" SKID")
Case $Loose = 6 AND $CRTN > 1 AND $PKG > 1
Send($CRTN&" CARTONS ON "&$PKG&" SKIDS")
EndSelect

Is there a way to make the following code better/more elegant?

Is there an error trap(s) that I'm missing? Thanks in advance.

Link to comment
Share on other sites

Looks alright to me, you could use a function I suppose and tuck it away somewhere.

$Loose = MsgBox(4, "Loose?", "Are the cartons on a skid?")

$CRTN = 1

$PKG = 1

WinActivate("Untitled - Notepad")

Switch _Select($Loose, $CRTN)
    Case  1
        Send($CRTN & " LOOSE CARTON")
    Case 2
        Send($CRTN & " LOOSE CARTONS")
    Case 3
        Send($CRTN & " CARTON ON " & $PKG & " SKID")
    Case 4
        Send($CRTN & " CARTONS ON " & $PKG & " SKID")
    Case 5
        Send($CRTN & " CARTONS ON " & $PKG & " SKIDS")
EndSwitch

Func _Select($Loose, $CRTN)

    Select
        Case $Loose = 7 And $CRTN = 1
            Return 1
        Case $Loose = 7 And $CRTN > 1
            Return 2
        Case $Loose = 6 And $CRTN = 1 And $PKG = 1
            Return 3
        Case $Loose = 6 And $CRTN > 1 And $PKG = 1
            Return 4
        Case $Loose = 6 And $CRTN > 1 And $PKG > 1
            Return 5
    EndSelect
EndFunc   ;==>_Select

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

More often than not there is no need to use Select...EndSelect as Switch...EndSwitch is a better choice.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

JohnOne, I have two more questions.

How come there is no need to include $PKG these two lines?

Switch _Select($Loose, $CRTN)

and

Func _Select($Loose, $CRTN)

Also, how do I place the function out of the way of the main body of the script?

For example:

$Loose = MsgBox(4, "Loose?", "Are the cartons on a skid?")
$CRTN = 1
$PKG = 1
code
code
;send carton and skid count to Notepad
_Select($Loose,$CRTN)
code
code
Msgbox(0,"Done","Finished!")



WinActivate("Untitled - Notepad")

Switch _Select($Loose, $CRTN)
Case 1
Send($CRTN & " LOOSE CARTON")
Case 2
Send($CRTN & " LOOSE CARTONS")
Case 3
Send($CRTN & " CARTON ON " & $PKG & " SKID")
Case 4
Send($CRTN & " CARTONS ON " & $PKG & " SKID")
Case 5
Send($CRTN & " CARTONS ON " & $PKG & " SKIDS")
EndSwitch

Func _Select($Loose, $CRTN)

Select
Case $Loose = 7 And $CRTN = 1
Return 1
Case $Loose = 7 And $CRTN > 1
Return 2
Case $Loose = 6 And $CRTN = 1 And $PKG = 1
Return 3
Case $Loose = 6 And $CRTN > 1 And $PKG = 1
Return 4
Case $Loose = 6 And $CRTN > 1 And $PKG > 1
Return 5
EndSelect
EndFunc ;==>_Select
Link to comment
Share on other sites

And don't forget to add #include-once at the top of the UDF.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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