Jump to content
Sign in to follow this  

Variable (sometimes) not recognized as array

Recommended Posts


Hi all,

I am encountering a strange error in my script. To focus the problem, it is in a section that imports a file into an array variable using:

_FileReadToArray("c:\... \battery_info.out", $bat).

where $bat is defined earlier as a local variable, with no array designation [ ]. Later on I pose a conditional:

If $bat[17]="some text" Then


At this point, when I run the program, it sometimes recognizes $bat as an array, and sometimes it tells me that $bat is not an array, so I cannot couple it with [17]. Usually I run the program once, get the error message (program crashes), run again and it works fine. If there are a few minutes in between tries, I get the error again, even if the computer hasn't restarted.

What might cause this behavior?


Share this post

Link to post
Share on other sites

Without having a chance to look at a failing reproducer or actual code, it'll be very hard to say.

Wild guess: you use $bat = some_function_returning_array(whatever) but this function sometimes fails and $bat is then not an array.

Use error checking to avoid that kind of situation.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post

Link to post
Share on other sites

Use error checking after _FileReadToArray. If this command fails $bat will still be a simple variable. If you then try to acces the variable as an array you get an error.

My UDFs and Tutorials:


Active Directory (NEW 2018-12-03 - Version - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki

ADO - Wiki


Share this post

Link to post
Share on other sites

Hi guys,

thanks for your comments.

I didn't post the entire code because it is pretty long (in my perspective...), and much of it deals with GUI stuff.

A little elaboration: I am running some lab experiments that include pressurized vessels with in-situ pH probes. The data is stored in a Campbell Scientific Datalogger, and can be retrieved to an actual file on a computer only manually, through a free Campbells Scientific program. If you want the retrieving to be automated, you need to pay >$700 for their fancy software.

So instead I found out about AutoIt a few weeks ago and wrote a program that retrieves the data automatically every 30 seconds (while 1 loop), and plots the last data up to 12 hours back. It is also designed to send me a text message to my phone if there is pressure loss or if the power goes out in the lab (lab laptop's battery is discharging).

The actual battery status check is done through an external VBS script that produces an output file with a bunch of lines. This output file is then imported into my program, into $bat, using the command:

_FileReadToArray("c:\... \battery_info.out", $bat)


If $bat[17] = " Discharging = True" Then

[start alert procedure, send text message, bla bla bla]


Again, $bat is not used at any point prior to the loop, it is only declared as "Local $bat"

What I don't understand is why the program sometimes works and sometimes doesn't. I guess I'm not looking only to eliminate the problem but also to understand why it happens for future works. I can publish the enitre code, if it helps, but since it was not designed for a commercial project it is fairly ugly (structurally).




Oh, wait... The problem may not be with AutoIt, but with the script that checks for the battery status. I think there is some sort of a delay from the time of check to the time of print (of output file), which may cause the error. I'll get back to you on that...

Thanks again!

Share this post

Link to post
Share on other sites

Ok fellas, figured out what the problem was.

It wasn't the external script that checks for the battery status, but rather the command from AutoIt that ran it. I used:

RunWait("C:...cscript battery_info_no_echo.vbs //NoLogo")

This sometimes caused the VBS script not to run at all, and therefore no actual file was imported into $bat, which cause the array error. I wondered if the VBS script was not running sometimes becasue cscript is an integral part of Windows, and is actually some sort of a DOS command. So I switched to:

RunWait(@ComSpec & " /c " & "cscript battery_info_no_echo.vbs //NoLogo")

And now it runs great.

Thanks guys for your willing to help.

As an AutoIt newbie I must say that the AutoIt online documentation is superb. I often find myself finding a useful function while searching for another function that does something similar but not quite.

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