stealth Posted February 27, 2013 Posted February 27, 2013 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.
JohnOne Posted February 27, 2013 Posted February 27, 2013 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.
guinness Posted February 27, 2013 Posted February 27, 2013 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 parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018
stealth Posted February 27, 2013 Author Posted February 27, 2013 Thanks John and guinness. and sorry i didn't indent the code in my OP.
stealth Posted February 28, 2013 Author Posted February 28, 2013 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: expandcollapse popup$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
JohnOne Posted February 28, 2013 Posted February 28, 2013 Never noticed it, was just an example, just add it to both after another comma. Put it in a file called MyFuncs.au3 or something and add #include "MyFuncs.au3" AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
guinness Posted February 28, 2013 Posted February 28, 2013 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 parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now