Sign in to follow this  
Followers 0
Salo

Simplify array sequence

9 posts in this topic

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

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by orbs
1 person likes this

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

So lots of guigetmsg is likely what is breaking stuff?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0