Jump to content

Simplify array sequence


Recommended Posts

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

@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 by orbs

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

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

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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

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