Jump to content

array tutorial


peter1234
 Share

Recommended Posts

I hope this will be of help to someone. Shows how to make array, save array to file, read array from file.

;**** array tutorial by peter1234 ****
;requires beta AutoIt
#include <GuiConstants.au3>
#include <Array.au3>
#include <file.au3>
;************
;make array
;************
Dim $MyArray[1]
For $n = 101 to 105
     _ArrayAdd($MyArray, $n/10 )
Next
$MyArray[0] = Ubound($MyArray) - 1
_ArrayDisplay($MyArray, "array")
;************************
;save array to text file
;************************
FileChangeDir (@scriptdir )
FileDelete ("array.txt")
FileOpen("array.txt",1)
For $x = 0 to $MyArray[0]
FileWrite("array.txt", $MyArray[$x] )
If $x<>$MyArray[0] then FileWrite("array.txt", @CRLF )
Next
FileClose("array.txt")
MsgBox(0, "", "array written to text file   ")
;**************************
;read array from text file
;**************************
FileChangeDir (@scriptdir )
If FileExists ("array.txt")<>1 then 
    MsgBox(0, "ERROR", "Array text file not found   ")
    Exit
    EndIf
Dim $NewArray
FileOpen("array.txt",0)
If Not _FileReadToArray("array.txt",$NewArray) Then
   MsgBox(4096,"Error", " Error reading text file to $NewArray   error:" & @error)
   EndIf
FileClose("array.txt")   
;*************************
;display NewArray as read
;*************************
_ArrayDisplay($NewArray, "as read")   
;************************
;display fixed NewArray
;************************
_ArrayDelete ( $NewArray, 0 )
_ArrayDisplay($NewArray, "fixed") 
Exit

Simplified with suggestion by CHRIS95219

;**** array tutorial by peter1234 (simplified) ****
;requires beta AutoIt
#include <GuiConstants.au3>
#include <Array.au3>
#include <file.au3>
;************
;make array
;************
Dim $MyArray[1]
For $n = 101 to 105
     _ArrayAdd($MyArray, $n/10 )
Next
$MyArray[0] = Ubound($MyArray) - 1
;***********************
;display original array 
;***********************
_ArrayDisplay($MyArray, "array")
;************************
;save array to text file
;************************
_FileWriteFromArray("array.txt" , $MyArray,1)
MsgBox(0, "", "array was written to text file   ")
;**************************
;read array from text file
;**************************
Dim $NewArray
If Not _FileReadToArray("array.txt",$NewArray) Then
   MsgBox(4096,"Error", " Error reading Array    error:" & @error)
   Exit
EndIf
;**********************************
;display array read from text file
;**********************************
_ArrayDisplay($NewArray, "as read")
Edited by peter1234
Link to comment
Share on other sites

How about showing how it's done with out UDFs

i.e.

Example 1:

Dim $MyArray[1]
For $n = 1 to 5
     ReDim $MyArray[UBound($MyArray) + 1]; preserves the values in the array instead of removing the values while resizing an array
Next
$MyArray[0] = Ubound($MyArray) - 1; use index zero as size holder
MsgBox(0,"Array", "Size: " & $MyArray[0] & ", excluding zero index in this case used for size of array")

Example 2:

Dim $MyArray[1]
For $n = 0 to 3
     ReDim $MyArray[UBound($MyArray) + 1]; preserves the values in the array instead of removing the values while resizing an array
Next
MsgBox(0,"Array", "Size: " & UBound($MyArray) )
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

  • 1 year later...

I have been really wanting to be able to make this work give me a hand with the code I can't make it work!

#include <Array.au3>
Dim $MyArray

_ArrayAdd($MyArray,"Number Ten")
_ArrayAdd($MyArray,"Number Nine")
_ArrayAdd($MyArray,"Number Eight")
_ArrayAdd($MyArray,"Number Seven")
_ArrayAdd($MyArray,"Number Six")
_ArrayAdd($MyArray,"Number Five")
_ArrayAdd($MyArray,"Number Four")
_ArrayAdd($MyArray,"Number Three")
_ArrayAdd($MyArray,"Number Two")
_ArrayAdd($MyArray,"Number One")
For $n = 1 to 5
     ReDim $MyArray[UBound($MyArray) + 1]; preserves the values in the array instead of removing the values while resizing an array
Next
$MyArray[0] = Ubound($MyArray) - 1; use index zero as size holder
MsgBox(0,"Array", "Size: " & $MyArray[0] & ", excluding zero index in this case used for size of array")

AutoIt changed my life.

Link to comment
Share on other sites

Hi, if someone hasn't done it yet;

;  arraytute.au3
#include <Array.au3>
$MyArray=_ArrayCreate("")

;~ Dim $MyArray

_ArrayAdd($MyArray,"Number Ten")
_ArrayAdd($MyArray,"Number Nine")
_ArrayAdd($MyArray,"Number Eight")
_ArrayAdd($MyArray,"Number Seven")
_ArrayAdd($MyArray,"Number Six")
_ArrayAdd($MyArray,"Number Five")
_ArrayAdd($MyArray,"Number Four")
_ArrayAdd($MyArray,"Number Three")
_ArrayAdd($MyArray,"Number Two")
_ArrayAdd($MyArray,"Number One")
;~ For $n = 1 to 5
;~      ReDim $MyArray[UBound($MyArray) + 1]; preserves the values in the array instead of removing the values while resizing an array
;~ Next
$MyArray[0] = Ubound($MyArray) - 1; use index zero as size holder
_ArrayDisplay($MyArray)
MsgBox(0,"Array", "Size: " & $MyArray[0] & ", excluding zero index in this case used for size of array")
Best, randall;

better to follow;

Edited by randallc
Link to comment
Share on other sites

#include <Array.au3>
local  $MyArray[10]=["Number One","Number Two","Number Three","Number Four","Number Five","Number Six","Number Seven","Number Eight","Number Nine","Number Ten"]
_ArrayDisplay($MyArray)

Edited by randallc
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...