Jump to content

Help with functions that call themselves


Recommended Posts

Hello everyone.  Consider this autoit code:

 

$source = _INetGetSource(ClipGet ())

            $reg = StringRegExp($source, 'source src=\"https://[A-Za-z0-9\.\-/_]+\.mp4\"', 3)
            $theurl = Null
            If UBound($grr) > 1 Then

                $theurl = findsmall ( $reg )
            Else
                $theurl = StringTrimLeft(StringTrimRight($reg[0], 1), 12)
                $file = InetGetSize ( $theurl, $INET_IGNORESSL )
                If $file = 0 Then
                    $theurl = Null
                EndIf

            EndIf


            MsgBox ( 1, "", $theurl )


            Func findsmall ( $exp )
    $grr = $exp
    _ArrayDisplay ( $grr )
    If UBound ( $exp ) = 0 Then
        Return Null
    Else
        $index = Null
        $num = Null
        For $t = 0 To UBound($grr) - 1 Step 1
            $regnum = StringRegExp(StringTrimLeft(StringTrimRight($grr[$t], 1), 12), "\d{1,3}x\d{1,3}", 1)
            If $num = Null Or Number(StringTrimRight($regnum[0], 4)) < $num Then
                $index = $t
                $num = Number(StringTrimRight($regnum[0], 4))
            EndIf
        Next
        $testurl = StringTrimLeft(StringTrimRight($grr[$index], 1), 12)
        MsgBox ( 1, "", $testurl )
        $file = InetGetSize ( $testurl, $INET_IGNORESSL )
        If $file = 0 Then
            _ArrayDelete ( $grr, $index )
            findsmall ( $grr )
        Else
            Return $testurl
        EndIf

    EndIf

EndFunc

 

In this script, I am trying to scan a web page's source html to find links to video files.  In the event that multiple video files are detected, the custom function I have written, "findsmall", will run.  The purpose of findsmall is to find the video file with a valid url and has the smallest resolution based on the url string.  To do this, I loop through the urls, in the array and determine their size based on text pulled from the url string.  After I find the video with the smallest resolution, I run Inetgetsize to verify that the link leads to a valid url.  If the url does not link to a valid url, then I run "_arraydelete" to delete the index in the array which held the invalid video url and re-run the function with the new array.  The script works as intended, but I am running into an issue in instances where the function is re-run because the first video link is not valid.  Normally, I would expect that the function would, if it encountered an invalid url, would continue to run itself, which would also reduce the size of the array, until a valid url is found or all items in the array are deleted, and then take the value returned by the re-run function and assign that to the variable which initially called it.  In other words, referencing the above code, if the function had to re-run itself due to an invalid url:

findsmall ( $grr )

Then I would expect that, if that run of the function returned a valid url, that value would be assigned to the original variable which was assigned to equal the call of the original function:
 

$theurl = findsmall ( $reg )

But that is not what is happening, oddly enough.  When the embedded function re-runs itself, it is almost as if autoit thinks it is an entirely seperate function, meaning that, in order to retrieve the value returned by the re-run function, I would need to assign the re-run function as its own variable: 

; this value would equal 0 if the function calls itself, as the initial run of the function didn't return any value
                $theurl = findsmall ( $reg )
            Func findsmall ( $exp )
    $grr = $exp
    _ArrayDisplay ( $grr )
    If UBound ( $grr ) = 0 Then
        Return Null
    Else
        $index = Null
        $num = Null
        For $t = 0 To UBound($grr) - 1 Step 1
            $regnum = StringRegExp(StringTrimLeft(StringTrimRight($grr[$t], 1), 12), "\d{1,3}x\d{1,3}", 1)
            If $num = Null Or Number(StringTrimRight($regnum[0], 4)) < $num Then
                $index = $t
                $num = Number(StringTrimRight($regnum[0], 4))
            EndIf
        Next
        $testurl = StringTrimLeft(StringTrimRight($grr[$index], 1), 12)
        MsgBox ( 1, "", $testurl )
        $file = InetGetSize ( $testurl, $INET_IGNORESSL )
        If $file = 0 Then
    ; This would be the variable that would contain the value that I am looking for, but this is not where I need the value.  I need the value assigned to the original variable in the main part of the script: $theurl
            $NEWVALUE = _ArrayDelete ( $grr, $index )
            findsmall ( $grr )
        Else
            Return $testurl
        EndIf

    EndIf

EndFunc

Is this by design?  If so, is there a way that I can get any instances of the function calling itself to assign the value it returns to the original variable ($theurl)?  Thanks in advance.

Edited by MattHiggs
Link to post
Share on other sites

Recursion is more complex that most ppl think.  I would strongly recommend that you remove the recursion and just do a simple loop.  As a suggestion, you can test the validity of the url immediately after finding a lower size file, if it is good you keep the index, if not you just continue with the next file.  Simple loop.

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...