Jump to content

Arrays in files


Recommended Posts

Hi, I'm working on a project that allows me to create an array of information and save it to a text file when I click a button on a GUI.

my code so far looks like this:

Func buttonCA()
    $P1= ;some random numbers here.
    $P2= ;some more random numbers here.
    $split = ","
    Local $arr[2] = [$Px,$Py]
    $pfile = FileOpen($FileName & "Array.txt", 1)
    FileWrite($pfile, $arr[0] & $split & $arr[1] & @CRLF)
EndFunc

that was to create the arrays.

$line = FileReadLine($pfile,)
StringSplit($line,",")
$xline = $arr[0]
$yline = $arr[1]
$P1 = $xline;
$P2 = $yline;

This is to read from the file.

However, its giving me:

$P1 = $arr[0]
$P1 = ^ERROR

Error:Variable used without being declared.

And I've tried putting

$P1 = 0
$P2 = 0

earlier on in the script but it's still not working, same Error msg.

Thanks.

Hello, World!... LAME lol

Link to comment
Share on other sites

_ArrayToString and reverse StringSplit should do the trick.

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

No, that doesn't work, still gives me the same error:

$P1 = $arr[0]
$P1 = ^ERROR

Error:Variable used without being declared.

Thanks for the suggestion though. polps :blink:

Edited by maestro

Hello, World!... LAME lol

Link to comment
Share on other sites

Xenobiologist,

could you show me how I might incorporate that into the script, I've just started playing around with arrays in AutoIt and am still unfamiliar as-to how they are written-in/setup.

Hello, World!... LAME lol

Link to comment
Share on other sites

You can read a little bit about data serialization and deserialization. You can't persist array variable to a file and expect to get it back. You serialize the variable content in the form you like and deserialize it back, both using your preferred serialization routine.

For example, if you're persisting a [x,y] coordinate to file as "123, 456" (without quotes), you can get it back using:

$arr = StringSplit($line1, ",", 2)

.. somewhere else in code so $arr[0] is 123 and $arr[1] is 456, on success.

Link to comment
Share on other sites

I just managed to find a couple of minutes. This might help.

#include <File.au3>
#include <Array.au3>

Dim $av_Array1[6] = [5, "element 1","element 2","element 3","element 4","element 5"] ; First element contains number of entries

_ArrayDisplay($av_Array1, "Array 1") ; Display 1st array

$sFile = @ScriptDir & "\Test.txt"
_FileWriteFromArray($sFile, $av_Array1, 1) ; Write array to file starting at element 1.

Dim $av_Array2 ; Declare the array to read to
_FileReadToArray($sFile, $av_Array2) ; Read array from file

_ArrayDisplay($av_Array2, "Array 2") ; Display 2nd array which has been read from file
Link to comment
Share on other sites

So I set it up like you suggested, but it's saying the variable is not declared.

the write to file code looks like this:

$Px= ;Xcord
$Py= ;Ycord
$split = ","
FileWrite ($FileName & ".txt", $Px & $split & $Py & @CRLF)

and this is what the file format looks like

1320,100

1240,89

1024,48

954,26

...

And the recovey(read code) looks like this:

$arr = StringSplit($line1, ",", 2)

$Xcord = $arr[0];
$Ycord = $arr[1];

however its still presenting me with the same problem.

Thanks.

Hello, World!... LAME lol

Link to comment
Share on other sites

Unable to get it working. When I run it, it doesn't save to the file its supposed to.

And then it gives me the "undeclared" Error, maybe it's something small. Here's my code, perhaps I made a silly mistake.

Write to:

Dim $av_Array1[3] = [2, $Px, $Py] ; First element contains number of entries
    _ArrayDisplay($av_Array1, "Array 1") ; Display 1st array
    $sFile = @ScriptDir & $FileName & ".path"
    _FileWriteFromArray($sFile, $av_Array1, 1) ; Write array to file starting at element 1.

Read from:

Dim $av_Array1 ; Declare the array to read to
_FileReadToArray($sFile, $av_Array1) ; Read array from file

_ArrayDisplay($av_Array1, "Array 2") ; Display 2nd array which has been read from file
$posX = $arr[0];
$posY = $arr[1];

Hello, World!... LAME lol

Link to comment
Share on other sites

#include <Array.au3>
#include <File.au3>
$Px = 10
$Py = 20
$FileName = '\file'

Dim $av_Array1[3] = [2, $Px, $Py] ; First element contains number of entries
_ArrayDisplay($av_Array1, "Array 1") ; Display 1st array
$sFile = @ScriptDir & $FileName & ".path"
_FileWriteFromArray($sFile, $av_Array1, 1) ; Write array to file starting at element 1.

Dim $av_Array1 ; Declare the array to read to
_FileReadToArray($sFile, $av_Array1) ; Read array from file

_ArrayDisplay($av_Array1, "Array 2") ; Display 2nd array which has been read from file
$posX = $av_Array1[1];
$posY = $av_Array1[2];

MsgBox(64, 'info', '$posX= ' & $posX & ' $posY= ' & $posY)

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

#include <Array.au3>
#include <File.au3>
$FileName = '\file'

Dim $points_A = StringSplit("10,20,20,30,40,50,60,70,80", ',', 2)
_ArrayDisplay($points_A, "Array 1") ; Display 1st array
$sFile = @ScriptDir & $FileName & ".path"
_FileWriteFromArray($sFile, $points_A, 1) ; Write array to file starting at element 1.

Dim $points_A2 ; Declare the array to read to
_FileReadToArray($sFile, $points_A2) ; Read array from file

_ArrayDisplay($points_A2, "Array 2") ; Display 2nd array which has been read from file

For $i = 1 To UBound($points_A2) - 1 Step 2
    MsgBox(64, 'info', "point" & Floor($i / 2 + 1) & ': $posX= ' & $points_A2[$i] & ' $posY= ' & $points_A2[$i + 1])
Next

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

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