Question

I have created a simple script in AutoIT that checks if a string exists or not, if it exists it will show label else it will show another label here is the script :

$text = $oHTTP.ResponseText
$status = $oHTTP.Status
If $status = 200 Then
    $array = _StringBetween($text, '<span class="d1">', "</span>")
    If $array[0] == "" Then
        GUICtrlSetState($v2l1, $GUI_SHOW)
    ELSE
        GUICtrlSetState($v2l2, $GUI_SHOW)
    EndIf
Else
    ConsoleWrite(@error)
EndIf

if it find anything then the label will appear but if it didn't find the string it gives me an error and exit:

Subscript used on non-accessible variable.: If $array[0] == "" Then If $array^ ERROR

So is there anyway i can fix this ? I mean if $array didn't find the string it make $v2l2 visible.

Thanks in advanced.

Was it helpful?

Solution

You should use IsArray to test whether you received a valid array or not. This procedure is recommended whenever you call a function that returns an array.

If $status = 200 Then
    $array = _StringBetween($text, '<span class="d1">', "</span>")
    If IsArray($array) Then
        ; process the array
    Else
        ; handle the error
    EndIf
EndIf

OTHER TIPS

Use @error which is set by _StringBetween(), this is proper AutoIt coding.

If $status = 200 Then
     $array = _StringBetween($text, '<span class="d1">', "</span>")
     If Not @error Then
         ; process the array
     Else
         ; Handle the error
     EndIf
EndIf
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top