【问题标题】:If _StringBetween didn't find the value如果 _StringBetween 没有找到值
【发布时间】:2014-05-06 22:19:39
【问题描述】:

我在 AutoIT 中创建了一个简单的脚本,用于检查字符串是否存在,如果存在,它将显示标签,否则它将显示另一个标签,这里是脚本:

$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

如果它找到任何东西,那么标签会出现,但如果它没有找到字符串,它会给我一个错误并退出:

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

那么有没有我可以解决这个问题?我的意思是如果 $array 没有找到它使 $v2l2 可见的字符串。

提前致谢。

【问题讨论】:

    标签: autoit winhttprequest


    【解决方案1】:

    您应该使用IsArray 来测试您是否收到了有效的数组。每当您调用返回数组的函数时,都建议使用此过程。

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

    【讨论】:

    • 您是对这个广泛报道的问题的第一个解释,它帮助了我。谢谢!
    • IsDeclared 不适用于返回的数组时,这是您需要使用的。我希望他们在 IsDeclared 上记录下来;这是一个严重的错误。
    【解决方案2】:

    使用由_StringBetween()设置的@error,这是正确的AutoIt编码。

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

    【讨论】:

    • 我用过@error,但用另一种方式:),无论如何,谢谢,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多