Salo Posted June 29, 2016 Share Posted June 29, 2016 I have this code I wrote and I was trying to figure out how to collapse/simplify it. I have 9(plan to add more, too) of these exact duplicate if/elseif blocks of code, other than the $ac00, 01, 10, 11, etc. If $sName = $ac[0][0] Then $CD = $ac[0][1] Switch GUIGetMsg() Case $iOKButton While 1 *snip* WEnd EndSwitch ElseIf $sName = $ac[1][0] Then $CD = $ac[1][1] Switch GUIGetMsg() Case $iOKButton While 1 *snip* WEnd EndSwitch ElseIf $sName = $ac[2][0] Then $CD = $ac[2][1] Switch GUIGetMsg() Case $iOKButton *snip* EndSwitch Link to comment Share on other sites More sharing options...
orbs Posted June 29, 2016 Share Posted June 29, 2016 (edited) @Salo, welcome to AutoIt and to the forum. first, retract your check for $iOKButton, so the if parts happen after someone pressed the OK button. then, you can search the array to find the index match for $sName, and use it to assign the matching value to $CD. something like this: Switch GUIGetMsg() Case $iOKButton For $i = 0 To UBound($ac) - 1 If $ac[$i][0] = $sName Then $CD = $ac[$i][1] * snip * EndIf Next EndSwitch Edited June 29, 2016 by orbs Salo 1 Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
Salo Posted June 29, 2016 Author Share Posted June 29, 2016 Oh man, that works and looks way better. Thanks for the simplify and thanks for the improvements. But what does it matter where the OK button is? Why is your method better? I mean I know it's better, because before, the OK button would kind of stick, you'd need to press it a few times to make it go. Your code fixed that also. But why? So from (counting variable)0 to max of ac(-1), and "next" acts as a +1. Right? Link to comment Share on other sites More sharing options...
spudw2k Posted June 29, 2016 Share Posted June 29, 2016 I believe his point is that you shouldn't have to call GUIGetMsg over and over in your oringal code. Simply doing it once and acting upon it is much more efficient. Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
Salo Posted June 29, 2016 Author Share Posted June 29, 2016 So lots of guigetmsg is likely what is breaking stuff? Link to comment Share on other sites More sharing options...
spudw2k Posted June 29, 2016 Share Posted June 29, 2016 Breaking...doubtful, but depends on the code; mostly it's just inefficient. There's not enough code or detail on what your script does for me to make a good judgement. Best I can say is there is no reason I am aware of why you would want (or need) to do a GUIGetMsg more than once per main loop. Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
Salo Posted June 29, 2016 Author Share Posted June 29, 2016 Regardless what it was, I'll keep that in mind for future scripting- GetGuiMsg is not good to use in this fashion. Thanks for all your help guys. Link to comment Share on other sites More sharing options...
orbs Posted June 30, 2016 Share Posted June 30, 2016 GUIGetMsg has a built-in delay, to prevent CPU overhead when a script does nothing but wait for user activity. it also works like a queue, reading events sequentially and processing them in order. so when you call GUIGetMsg in a section of code that expects a certain input, but the input is actually different, then the code will not be executed - but neither will the section of code that does expect the said input, because at next read GUIGetMsg returns zero. anyway, apply some common sense - it's a bad practice to call something several times if you need it only once. this at least is ought to be obvious. Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
Salo Posted June 30, 2016 Author Share Posted June 30, 2016 Cool, I didn't know it had a built in delay, so that's what was actually breaking stuff then. And it was obvious to me, which was why I was trying to figure out how to simplify it. Link to comment Share on other sites More sharing options...
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