Jump to content

Is there a memory queue for AutoIt?


Recommended Posts

I am trying to learn AutoIt and am impressed with its power but with that power comes a bit of a learning curve.

I am going through a file with 3000 records. Some of these records have information that I need to refer back to later. I don't know how many. In another programming language I have used I could put the information onto a memory queue and then when needed retrieve that information by record number or by search. I was only limited by available memory and only had to define the format of one queue record. Then I could add as many as I wanted.

I guess maybe what I am looking for is a variable length array so to speak where I don't have to give an upper bound.

Thanks,

Bob Roos

Link to comment
Share on other sites

Take a look in the help file under _FileReadToArray.

You do not need to set the upper bound.

Dim $aRecords

_FileReadToArray("error.log",$aRecords)

Thank you. That is close to what I want.

I will be extracting information out of SOME of the records of my input file and it is that information that I want to save.

So if 50 records out of 3000 have information then my array would have an upperbound of 50, Likewise if it had information from every record then there would be 3000 elements in my array.

_FileReadToArray has some way to put data into the next element of an unbounded array and that is what I am seeking.

I am a little confused because when I read the help for DIM they show dimensions. It gets a little hairer because normally the square brackets are used to denote an optional parameter, but in the case of DIM they are needed to show that this is an array.

Link to comment
Share on other sites

After getting the 3000 records into an array, make a second array and use _ArrayAdd to add to it. As for the DIM line and brackets - those two lines of code in my first post above came from the help file example for using _FileReadToArray. Since the _FileReadToArray function uses StringSplit, you need not declare $aRecords as an array - StringSplit will do it for you.

If the data to be extracted from the first array is small, then you could build a string with the delimiter of your choice - then use StringSplit to build your second array. Use the amperstand character to concatenate strings.

Maximum string length: 2,147,483,647 characters

for $i 1 to $aRecords[0]

if $aRecords[$i] = something then $newString &= $newString

next

$newArray = StringSplit($newString, "delimiter")

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

  • Moderators

It's the same in AutoIt as it is in other languages... just like any other language you have to create that array in memory first.

We use the standard Dim / Local / Global to declare our arrays and surround the original dimensions in square brackets.

Global $a_myArray[15].

The LBound is always 0, never a negative. [0] can contain any data you want, as the example that herewasplato has provided using _FileReadToArray [0] infact has a value, it contains the number of lines from the file read into the array.

To increase or decrease the dimensions we use Redim. There's no preserve need, it will preserve the records automatically, unless you reset the array Global

;Initiate Array
Global $a_myArray[15]

;; Set Value
$a_myArray[0] = "Apple"

;; Redim Preserve, decrease to 1 index
ReDim $a_myArray[1]
;; Verify Preserve
MsgBox(64, "Preserve Value", "$a_myArray[0] = " & $a_myArray[0])

;; Redim Preserve, increase to Random 100 to 1000 indexes (Shows arrays can be dynamic)
Global $n_Random = Random(100, 1000, 1)
ReDim $a_myArray[$n_Random]
;; Verify Preserve
MsgBox(64, "Preserve Value : Indexes = " & $n_Random, "$a_myArray[0] = " & $a_myArray[0])

;; Empty my array, then reset my array to original size
Global $a_myArray = "", $a_myArray[15]
;; Verify empty array
MsgBox(64, "Should be empty", "$a_myArray[0] = " & $a_myArray[0])

Keep in mind there is a UBound() function, that gives you the actual number of indexes (You must subtract 1 from it), but there is no LBound because you can not be below 0 as an lbound, if you need something else (a higher number than 0), you'll need to create array management functions on your own.

Edit:

You also don't need to reset an array like such $a_myArray = "", Re declaring the array with dimensions and without ReDim will automatically reset the array.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Smoke_N, your examples deal with explicitly dimensioned arrays while herewasplato's deal with undimensioned arrays. The help is vague on undimensioned arrays.

; Here's why I am a bit confused. Things don't seem to be entirely consistent. The _ArrayXXXXXX functions won't work with an undimensioned array but _FileReadToArray will. What does it do to turn the undimensioned array into an array? Maybe the question is what does it do to turn a single valued variable into an array?

CODE

#include <Array.au3>

#include <file.au3>

; test array stuff

Dim $testarray ; if I leave off the dimension here the _ArrayAdd and _ArrayDisplay functions are ignored without error but if I put in an explicit dimension (say [3]) then they work

; $testarray[0] = 1 ; I must declare $testarray with an explicit dimension to avoid an error here

_ArrayAdd($testarray,'2')

_ArrayDisplay($testarray," ")

;MsgBox(64,' ',$testarray[0] & ' ' & $testarray[1]) ; gives errors unless explicit dimension is present

Dim $t ; leave off any explicit dimensions

_FileReadToArray("c:\trace.txt",$t) ; happily reads in the file

_ArrayDisplay($t," ") ;and displays it

$t[0] = 12 ;this assignment works just fine without putting an explicit dimension on the array.

MsgBox(64,' ',$t[0]) ; displays the modified first element NOTE that $t was never declared with a dimension

Edited by BobRoos
Link to comment
Share on other sites

  • Moderators

Smoke_N, your examples deal with explicitly dimensioned arrays while herewasplato's deal with undimensioned arrays. The help is vague on undimensioned arrays.

; Here's why I am a bit confused. Things don't seem to be entirely consistent. The _ArrayXXXXXX functions won't work with an undimensioned array but _FileReadToArray will. What does it do to turn the undimensioned array into an array? Maybe the question is what does it do to turn a single valued variable into an array?

Hmm, An array has to be initiated no matter what, or it's not an array (it's just a simple variable). And it ALWAYS (Even _FileReadToArray) has a dimension (Good idea to check your include file and look at it for yourself.).

The difference I think you are confused on, is the array is created within these functions (behind the scenes) and you don't see it happening (them being initiated with dimensions).

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

...but _FileReadToArray will. What does it do to turn the undimensioned array into an array?...

"It" uses the StringSplit function which returns/creates/"DIMs" an array*.

Return Value

Returns an array, the first element ($array[0]) contains the number of strings returned, the remaining elements ($array[1], $array[2], etc.) contain the delimited strings.

If no delimiters were found @error is set to 1, the count is 1 ($array[0]) and the full string is returned ($array[1]).

With the exception of _ArrayCreate [which is not in the v3.2.12.0 help file] all of the _Arrayxxxxxx functions are manipulating an existing array - so, one would expect that the array be "DIMmed" before calling those User Defined Functions (UDF).

You can submit a bugtrac if you can think of some verbiage that would clarify things in the help file. It is a great help file that gets tweaked at times.

*Just my guess - I could be wrong about how StringSplit does what it does.

[size="1"][font="Arial"].[u].[/u][/font][/size]

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