Jump to content

Calling functions in varying order


Recommended Posts

So, I have a .au3 file with 10 functions in it. Right now, the first one calls all the other functions, the second one gets the order that the last 8 should go in, and the last 8 are the ones that I want to have called in a specific, varying order.

My script runs every hour, and every time the order of what function should be called first (since it matters the order for my script) changes.

Here's the basic setup, with what I want to happen:

Func _9() ;all this does is call the below functions from another file
    _10() ;gets the $Order array

    #cs
        No point in making the rest of the functions... just there for structure, but I need a way
        to call them in order of the lowest value in $Order calling the lowest of these first
        _1()
        _2()
        _3()
        _4()
        _5()
        _6()
        _7()
        _8()
    #ce
EndFunc   ;==>_9

Func _10()
    #cs
        $Order = _StringBetween($Source, "s", "e")
        
        For $i = 0 to 7
        $Rep = _StringBetween($Order[$i], "SPANclass=", "/SPAN")
        $Rep = _StringBetween($Rep[0], ">","<")
        $Order[$i] = Int($Rep[0])
        Next
        
        So, this is what's going on in my script, but since it requires a bit of stuff, we'll
        just leave this here for reference, and declare $Order as it currently is...
        
        I don't want to change the order of $Order, because I still want to be able to say that
        if $Order[1] is lowest then $Order[5], it should call _2() first, then _6()
    #ce
    Dim $Order[8] = [13, 20, 25, 12, 13, 21, 0, 0]
EndFunc   ;==>_10

The code explains what's going on and what I want.

I don't want to change the order of $Order, because I still want to be able to say that if $Order[1] is lowest then $Order[5], it should call _2() first, then _6()

I don't know if that's enough info, but I'll add more if it's needed

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

Well, I figured out a lame way to solve it... I knew I could do something like this... I just didn't know how to set it up, and I'm sure there's a better way. Requires quite a bit of manual set up

#include <Array.au3>

Global $Order, $Func

_9()

Func _9() ;all this does is call the below functions from another file
    _10() ;gets the $Value array, and creates the $Order array
    _Order() ;Sorts the $Order array from lowest to highest, sends the lowest to $Func, then resets that value to 999 (bottom of the list)
    #cs
    Do
        If $Func = 1 Then _1()
        If $Func = 2 Then _2()
        If $Func = 3 Then _3()
        If $Func = 4 Then _4()
        If $Func = 5 Then _5()
        If $Func = 6 Then _6()
    Until $Func = 0
    #ce
EndFunc   ;==>_9

Func _10()
    #cs
        $Value = _StringBetween($Source, "s", "e")

        For $i = 0 to 7
        $Rep = _StringBetween($Value[$i], "SPANclass=", "/SPAN")
        $Rep = _StringBetween($Rep[0], ">","<")
        $Value[$i] = Int($Rep[0])
        Next

        So, this is what's going on in my script, but since it requires a bit of stuff, we'll
        just leave this here for reference, and declare $Value as it currently is...
    #ce
    Dim $Values[6] = [13, 20, 25, 12, 13, 21]
    Dim $Order[6][2] = [[$Values[0], 1],[$Values[1], 2],[$Values[2], 3],[$Values[3], 4],[$Values[4], 5],[$Values[5], 6]]
EndFunc   ;==>_10

Func _Order()
    _ArraySort($Order) ;Sorts the list, lowest to highest...
    $Func = $Order[0][1] ;Gets the second array value, which is the function number...
    $Check = $Order[0][0] ;Check to see if a duplicate is coming up...
    $Order[0][0] = 999 ;Reset the value to send it to the bottom of the list...
    If $Check = 999 Then $Func = 0 ;If it's a duplicate, don't call a function, and ExitLoop.
EndFunc   ;==>_Order

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

Let me see if I get this properly. You have a series of functions, but you want to change the order in which they are called each time the loop is run. The order is controlled by a sequence number in the array $Order. So long as the sequence numbers are kept to a reasonable level, this can be managed fairly efficiently.

Func Dispatch(ByRef Const $Order)
    ; Returns false on failure ($Order is not formed properly); true on success
    Local $nMin, $nMax, $I, $K

    ; Verify that $Order is a proper array
    If UBound($Order, 0) <> 1 Then
        ; Not the proper type of array.  Should be one-dimensional.
        Return False
    ElseIf UBound($Order) <> 8 Then
        ; Array is not the correct size.  It should have eight elements.
        Return False
    EndIf

    ; Determine the minimum and maximum values in the $Order array.
    $nMin = $Order[0]  ; For now the first is the smallest
    $nMax = $Min       ; It is also the biggest.
    For $I = 1 To UBound($Order) - 1
        If $nMin > $Order[$I] Then $nMin = $Order[$I]
        If $nMax < $Order[$I] Then $nMax = $Order[$I]
    Next
    For $K = $nMin to $nMax
        For $I = 0 To UBound($Order) - 1
            If $Order[$I] = $K Then
                Switch $I
                Case 0
                    _0()
                Case 1
                    _1()
                Case 2
                    _2()
                Case 3
                    _3()
                Case 4
                    _4()
                Case 5
                    _5()
                Case 6
                    _6()
                Case 7
                    _7()
                EndSwitch
            EndIf
        Next
    Next
    Return True
EndFunc

You should probably add checks to make sure your called functions actually succeed.

Edit: Add comments and above sentence.

Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Thanks for the reply, I'm about to go to bed, and I'm at a hotel with a crappy internet, so I can't test that right now, but I'll give it a go tomorrow or something. That looks pretty fancy... I'm sure it'll do what I want. Know anything about using proxies with _IE.au3 UDFs?

Oh, and before I go to bed, just fyi, it helps when you know how many things you want to do in your script. It's only 6 other functions to call apparently... discovered that on my plane ride here >_< but looking over your script quickly that looks like just a couple number changes?

Edited by mistersquirrle

We ought not to misbehave, but we should look as though we could.

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