Jump to content

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?

Thanks!

Link to comment
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)

Link to comment
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:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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)

and:

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

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

EndIf

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!

Link to comment
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.

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...