Jump to content
Sign in to follow this  
leuce

Error: Subscript used with non-Array variable

Recommended Posts

leuce

G'day everyone

I'm writing a script and getting a very strage error message:

"Subscript used with non-Array variable".

The script reads a URL from a text file URLs.txt and Inetgets it, then checks it for matches to a StringRegExp query. If there are no maches, the array should be empty, right?

$pageurl = FileReadLine("URLs.txt", $i)

$wikipage = InetGet($pageurl, "page.txt")

$result = StringRegExp($wikipage, '(?:nl\.wikipedia\.org/wiki/)(.+)(?:">Nederlands)', 1)

If $result[0] = 1 Then
FileWriteLine ($glossary, $pageurl & " :: " & $result[1])
EndIf

The error message relates to $result[0]. I want the FileWriteLine to be executed only if the array isn't empty.

Another question (not very important) is that it appears as if the local filename for an Inetget isn't optional as the manual states. If I don't specify the local filename, Autoit doesn't wait until the Inetget is completed. But that's not important right now.

Your help appreciated.

Samuel

Share this post


Link to post
Share on other sites
SadBunny

G'day everyone

I'm writing a script and getting a very strage error message:

"Subscript used with non-Array variable".

The script reads a URL from a text file URLs.txt and Inetgets it, then checks it for matches to a StringRegExp query. If there are no maches, the array should be empty, right?

$pageurl = FileReadLine("URLs.txt", $i)

$wikipage = InetGet($pageurl, "page.txt")

$result = StringRegExp($wikipage, '(?:nl\.wikipedia\.org/wiki/)(.+)(?:">Nederlands)', 1)

If $result[0] = 1 Then
FileWriteLine ($glossary, $pageurl & " :: " & $result[1])
EndIf

The error message relates to $result[0]. I want the FileWriteLine to be executed only if the array isn't empty.

Another question (not very important) is that it appears as if the local filename for an Inetget isn't optional as the manual states. If I don't specify the local filename, Autoit doesn't wait until the Inetget is completed. But that's not important right now.

Your help appreciated.

Samuel

Chances are your $result is not an array, presumably because the regexp did not trigger. Check the @error, or with IsArray, or with _ArrayDisplay, displaying the array so you know with what the rest of the functions are working with. (If you try to access an array element from a non-array variable you get this error.)

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
PsaltyDS

This adds error checking that will tell you what happened:

$pageurl = FileReadLine("URLs.txt", $i)
$wikipage = InetGet($pageurl, "page.txt")

$result = StringRegExp($wikipage, '(?:nl\.wikipedia\.org/wiki/)(.+)(?:">Nederlands)', 1)
Switch @error
    Case 0
        If $result[0] = 1 Then
            FileWriteLine($glossary, $pageurl & " :: " & $result[1])
        EndIf
    Case 1
        MsgBox(16, "Error", "No matches found.")
        Exit
    Case 2
        MsgBox(16, "Error", "Bad RegExp pattern used.")
        Exit
    Case Else
        MsgBox(16, "Error", "Unhandled case - @error = " & @error)
        Exit
EndSwitch

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
leuce

Thanks, that really works. Now I'll post my second question separately.

Share this post


Link to post
Share on other sites
leuce

Thanks, that really works. Now I'll post my second question separately.

Well, I thought it worked, and that I just have to figure out my regex, but no. Now I get the same error message again. Here is my code:

#include <File.au3>

$glossary = FileOpen("EN-NL.txt", 1)

$urllist = FileOpen("URLs.txt", 0)
$lines = _FileCountLines ("URLs.txt")

For $i = 1 to $lines

$pageurl = FileReadLine("URLs.txt", $i)

$wikipage = InetGet($pageurl, "foo.txt")
$wikipage2 = FileOpen ("foo.txt", 0)
$wikipage3 = FileRead ("foo.txt", 0)

$result = StringRegExp($wikipage3, '(?:nl\.wikipedia\.org/wiki/)(.+)(?:">Nederlands)', 1)

Sleep ("2000")

Switch @error
    Case 0
        If $result[0] > 0 Then
            FileWriteLine($glossary, $pageurl & " :: " & $result[1])
        EndIf
EndSwitch

Next

The regex should find

nl.wikipedia.org/wiki/Lounge">Nederlands

and add this to the array

Lounge

In this thread Mega suggested a different regex pattern, but that doesn't work either.

Any idea what's wrong here?

Thanks

Samuel

Share this post


Link to post
Share on other sites
PsaltyDS

Well, I thought it worked, and that I just have to figure out my regex, but no. Now I get the same error message again. Here is my code:

$result = StringRegExp($wikipage3, '(?:nl\.wikipedia\.org/wiki/)(.+)(?:">Nederlands)', 1)

Sleep ("2000")

Switch @error
    Case 0
        If $result[0] > 0 Then
            FileWriteLine($glossary, $pageurl & " :: " & $result[1])
        EndIf
EndSwitch
You are mixing two different problems here:

1. Bad error checking, which is still not fixed because your script ASSUMES you get a valid result from the StringRegExp() function. Put in some code so your script knows what to do when the StringRegExp() fails to find what you're looking for. Then it won't try to use a $result array that doesn't exist.

2. Broken RegExp, which keeps you from getting the match you want. I'm no help with RegExp, and you have yet to provide a sample string for $wikipage3 that we (Mega) can test it against. Minimize this snippet of code to just the RegExp function you need and use a static string example of what to search.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


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
Sign in to follow this  

×