Jump to content

Subscript used with non-Array variable when using StringRegExp and reading from a file


Recommended Posts

Hi, I recently started playing with AutoIT and the StringRegExp() function. My code was working when I used the function using a string within the code. For my purposes, I need to use the function on the content of a file, so I included the FileOpen function, used a handle to read the content to a variable and ran StringRegExp() on the variable. Since adding the file functions, I am getting a "Subscript used with non-Array variable.:" error. I incorporated IsArray() to check and my variable is actually not an array, but my understanding is that StringRegExp returns an array. Any thoughts?

Thanks for any help.

Things I've tried:

FileChangeDir (); but my code and file are in the same path

Changing the StringRegExp flags

******* Full Error message below:

"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\temp\Vpeek_func.au3"

C:\temp\Vpeek_func.au3 (23) : ==> Subscript used with non-Array variable.:

MsgBox(0, "It worked", $avg[0])

MsgBox(0, "It worked", $avg^ ERROR

>Exit code: 1 Time: 3.234

******* Code Starts here:

$mypeek = Run(@ComSpec & " /k appname > autoit.txt")

Sleep(3000)

; have tried declaring array and not declaring it

Dim $avg[10]

If $mypeek Then ; Run comparisons

$inFile = FileOpen("autoit.txt", 0) ; open my file in read mode

If $inFile = -1 Then ;make sure file successfully opened

MsgBox(0, "Error", "File open failed")

Exit

Else

$fileContent = FileRead ($inFile) ; Load content of entire file into handle

FileClose ($inFile)

$avg = StringRegExp($fileContent, "(?:Average = )(\d{1,}\d{1,}.\d{0,})", 2)

;If IsArray ($avg) Then

MsgBox(0, "It worked", $avg[0])

;Else

;MsgBox(0, "Not an array", "Check Array")

;Endif

EndIf

Else

Msgbox(0,"Status","An error occured with number: " & @error)

EndIf

Link to comment
Share on other sites

Try RunWait() vice Run(). You are opening the file within a couple milliseconds of starting the application. It probably hasn't written anything to it yet.

:blink:

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
Link to comment
Share on other sites

1) Use [ autoit][ /autoit] tags to display code properly.

2) Check your @Error error flag after the StringRegExp

3) If you take PsaltyDS's advice then I would take make the /k a /c in the run command because a RunWait will wait until the process closes and /k keeps the command window open while /c closes it when done.

Link to comment
Share on other sites

Thank you both for your help.

PsaltyDS: RunWait() using /k seems to cause my function to just hang and not continue; when I use /c it fails (@error non-zero). Because of this, I used Sleep.

ShawnW: You're right. I got an error after StringRegExp. Preliminary investigation shows that the problem is with the use of the non-capturing group (?:Average = ). The intriguing thing is that the same RegEx works fine when used on the string "Minimum = 0.647ms, Maximum = 4.961ms, Average = 1.7705ms" instead of the file handle. I will continue to investigate.

Link to comment
Share on other sites

Not sure if this is the issue, but if all your lines look like that then the regex you posted wont work. Also you don't need the non-capturing group. If you want to capture the average, and all lines look like that, then this should work.

#include <array.au3>

; ..... other code

$avg = StringRegExp($fileContent,"Average = (\d+\.\d+)",2)
If @error Then
    MsgBox(0,"Error Code",@error)
Else
    _ArrayDisplay($avg)
EndIf
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...