Jump to content

Recommended Posts

Posted

I'm hoping for some help. I have variables $A1, $A2, $A3, ..., $A'N' that each have an associated value of $v starting at 21. If any of the $A'N' variables are = $Red I want to loop through just those variables and ignore the rest but I cant think of a way to write the logic.

Here is what I've come up with for three $A'N'. This would run in a loop. Essentially if $A1 = $Red and $A2 = $Red this code will alternate between setting $v = 21 and $v=22 back and forth. If just $A2 = $Red it will set $v = 22 repeatedly. If all three $A1, $A2, $A3 are = $Red then it will alternate setting $v to 21, 22, 23 cycling through the values.

If $A1 = $Red and $A2 = $Red and $A3 = $Red Then
                  If $FlagThree = 0 Then
                     $v = 21
                  Endif
                  If $FlagThree = 1 Then
                     $v = 22
                  EndIf
                  If $FlagThree = 2 Then
                     $v = 23
                  EndIf
                  $FlagThree = $FlagThree + 1
                  If $FlagThree = 3 Then
                     $FlagThree = 0
                  Endif
               Elseif $A1 = $Red and $A2 = $Red Then
                  If $FlagA = 0 Then
                     $v = 21
                  Endif
                  If $FlagA = 1 Then
                     $v = 22
                  EndIf
                  $FlagA = $FlagA + 1
                  If $FlagA = 2 Then
                     $$FlagA = 0
                  Endif
               Elseif $A1 = $Red and $A3 = $Red Then
                  If $FlagB = 0 Then
                     $v = 21
                  Endif
                  If $FlagB = 1 Then
                     $v = 23
                  EndIf
                  $FlagB = $FlagB + 1
                  If $FlagB = 2 Then
                     $FlagB = 0
                  Endif
               Elseif $A2 = $Red and $A3 = $Red Then
                  If $FlagC = 0 Then
                     $v = 22
                  Endif
                  If $FlagC = 1 Then
                     $v = 23
                  EndIf
                  $FlagC = $FlagC + 1
                  If $FlagC = 2 Then
                     $FlagC = 0
                  Endif
               Elseif $A1 = $Red Then
                  $v = 21
               ElseIf $A2 = $Red Then
                  $v = 22
               ElseIf $A3 = $Red Then
                  $v = 23

How can I write this more efficiently and expand it to $A1 .. $A6 for example? If $A1, $A2, $A4, $A6 were = $Red it would cycle $v = 21, 22, 24, 26. 

Posted

Can you please explain which problem you try to solve with your script?
So we might propose a more efficient solution :) 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted
; for repeatedly setting some random proportion of arrayA[N] to "red"
SRandom(@AutoItPID)

Global $numberOfEntries = 6
Global $startingValue = 21  ; base output
Global $red = 123   ; arbitrary non-zero value designates "red"

Global $arrayA[$numberOfentries]

; infinite loop
While 1
    ConsoleWrite(@CRLF & "Changing " & $numberOfEntries & " A[N]'s..." & @CRLF)
    For $rc=1 To $numberOfentries
        $arrayA[$rc-1] = (Random()<.5)?($red):(0)   ; rc-1 because arrayA is indexed base-0
    Next
    Sleep(2000)

    ConsoleWrite("Cycling twice for this demo" & @CRLF)
    For $cc=1 To 2

        ; A[N] loop
        For $rc=1 To $numberOfentries
            If $arrayA[$rc-1]=$red Then
                ConsoleWrite("A item " & $rc & " is now 'red'; value v = " & $startingValue+$rc-1 & @CRLF)  ; -1 because $rc loop = base-1 and array index is base-0
                Sleep(1000)
            EndIf
        Next
        ConsoleWrite(@CRLF)
    Next
WEnd

 

Posted

It really depends on what your usecase is if its efficient.
example 1: Just make a map where you only put it in when the flag is red
example 2: Just make a bitwise compare

Example1()

Example2()

Func Example1()
        ; Declare a map and assign with various keys value pairs. Leaving 2 out
        Local $mRedFlags[]
        $mRedFlags["A1"] = 21
        $mRedFlags["A2"] = 22
;~         $mRedFlags["A3"] = 23
        $mRedFlags["A4"] = 24
;~         $mRedFlags["A5"] = 25
        $mRedFlags["A6"] = 26

        ; Retrieve the keys contained in the map. A zero-based one-dimensional array is returned.
        Local $aMapKeys = MapKeys($mRedFlags)
        For $i = 0 To UBound($aMapKeys) - 1
            consolewrite("Key: " & $aMapKeys[$i] & " Value: " & $mRedFlags[$aMapKeys[$i]] & " Variable Type: " & VarGetType($aMapKeys[$i]) & @CRLF) ; Display the variable type of the key i.e. integer or string.
        Next

EndFunc   ;==>Example

; Max 32 flags
Func example2()
    ;~ $mRedFlags=1+2+4+8+16+32+64+128+256+512+1024+2048  ; 12 flags on red
    $mRedFlags=1+2+8+16+64+128+256+512+1024+2048  ; flag 3,5 not in there so not on

    for $i=0 to 31
        if bitand($mRedFlags, 2^$i) Then
            consolewrite("A" & $i+1 & " value " & 21+$i & @CRLF)
        endif
    next
EndFunc

 

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
×
×
  • Create New...