Jump to content

Return variable from function


Go to solution Solved by cookiemonster,

Recommended Posts

Hey all, this must be a very silly question... Ive got a script below which I'm trying to return $sLine so it appears in the main results message box but it wont return it out of the function... Can anyone help me? 

Global $file = "C:\Users\James.Q\Desktop\MyFile.txt", $search = "the dog jumped"
Global $iLine = 0, $sLine = '', $iValid = 0

Call ("colorfinder",$file,$search,$iLine,$sLine,$iValid)

msgbox(4096,"Main result",$sLine)

Func colorfinder($file,$search,$iLine,$sLine,$iValid)
Global $hFile = FileOpen($file)
While 1
    $iLine += 1
    $sLine = FileReadLine($hFile)
    If @error = -1 Then ExitLoop

    ; test the line for the $search string until the flag $iValid is set
    If StringInStr($sLine, $search) Then
        $iValid = 1
    EndIf

    If $iValid Then
        $iValid = 1
        msgbox(4096,"result1",$sLine)
        ExitLoop
    EndIf
WEnd
msgbox(4096,"result2",$sLine)
Return $sLine
FileClose($hFile)

EndFunc ;colorfinder
Link to comment
Share on other sites

  • Solution

fixed it but just incase its any help to anyone...heres the fixed code:

Global $file = "C:\Users\James.Q\Desktop\MyFile.txt", $search = "the dog jumped"
Global $iLine = 0, $sLine = '', $iValid = 0

$ans = colorfinder($file,$search,$iLine,$sLine,$iValid)

msgbox(4096,"Main result",$ans)

Func colorfinder($file,$search,$iLine,$sLine,$iValid)
Global $hFile = FileOpen($file)
While 1
    $iLine += 1
    $sLine = FileReadLine($hFile)
    If @error = -1 Then ExitLoop

    ; test the line for the $search string until the flag $iValid is set
    If StringInStr($sLine, $search) Then
        $iValid = 1
    EndIf

    If $iValid Then
        $iValid = 1
        msgbox(4096,"result1",$sLine)
        ExitLoop
    EndIf
WEnd
msgbox(4096,"result2",$sLine)

FileClose($hFile)
Return $sLine
EndFunc ;colorfinder
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...