Jump to content

Simple- Store Array data in file and recall on different script in future


Recommended Posts

I am new and likely this is a very simple question to answer. Long story short I wish to save array data and recall it to a script. So that the array positions may change between computer but the operation itself remains the same.

Global $file = "test.txt"

$msg = msgbox(1,"Select place cursor over Ok radio", "Hit Ok / ENTER on keyboard to store position.")
If  $msg = 1 Then
    $pos = MouseGetPos()
    FileWriteLine($file, "x=" & $pos[0] & ", y=" & $pos[1])
    Beep(500, 500)
    MsgBox(0,"Next", "On to next step", 5)
Else
    MsgBox(0,"Canceled", "You canceled operation")
    Exit
EndIf

 

Then is the actual script:

Global $file = "test.txt"

MouseClick("PRIMARY", $file$line1$x [0], $file$line1$y [1], 1, 3)

etc

 

Storing seems to work fine. Not sure how to call the data or name it when it comes to the functioning script.

Link to comment
Share on other sites

I belive that FileRead is what you need, from the help file:

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = _WinAPI_GetTempFileName(@TempDir)

    ; Create a temporary file to read data from.
    If Not FileWrite($sFilePath, "This is an example of using FileRead.") Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
        Return False
    EndIf

    ; Open the file for reading and store the handle to a variable.
    Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False
    EndIf

    ; Read the contents of the file using the handle returned by FileOpen.
    Local $sFileRead = FileRead($hFileOpen)

    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)

    ; Display the contents of the file.
    MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & $sFileRead)

    ; Delete the temporary file.
    FileDelete($sFilePath)
EndFunc   ;==>Example

The first steps (store the data) you already did. Then you need to open the file and read it. To see how the content is being store, the above exemple shows it using MsgBox. All you need is to adapt the example.

Link to comment
Share on other sites

I am not 100% sure I follow. Does the file need to be read prior to referencing to it?

 

What I get in the text file generated is basically appending coordinates one after the other.

x=838, y=752
x=831, y=844
x=831, y=897

I am unsure how to identify each line while calling it. Be it have the program name it when it writes it or call based on line items. It would be simple if could call it back like an array. $pos[0]  $pos[1]

There is about 12 spots need "calibrated" so that once setup on a new computer I can just run it.

 

---> Found a function _filereadtoarray And 2D function applies to what I am doing. I think now just have to figure out how it works or find a script example I can pick apart to understand it.

Edited by Sleepyguy
Link to comment
Share on other sites

Look if this helps.

#include <WinAPIFiles.au3>
#include <Array.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = "test.txt" ;******put the full path of the test.txt******

    ; Open the file for reading and store the handle to a variable.
    Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False
    EndIf

    ; Read the current script file into an array using the FileOpen.
    Local $sFileRead = FileReadToArray($hFileOpen)
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file.
        Return False
    EndIf

    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)

    ; Display the contents of the file.
    _ArrayDisplay($sFileRead)
EndFunc   ;==>Example

This way the data will be put in the array.

Link to comment
Share on other sites

Thank you. So it appears I can reference to lines but not comma delimited numbers.

----------------------

Test.txt

838,752
831,844
831,897

-----------------------

MsgBox(0, "data", $sFileread[0] )

Displays the entire line:

838,752

(I need it as single coords as one is X other is Y. Unless program mouseclick can tell that there is two coords in one statement.)

--------------------

So do I attempt to split array or just make the output single lines when creating the coordinates? (I know just making new lines would be something I can already do but a pain to code each line)

Trying to get this to work:

; Rewrite 2D array with multiple delimiters
    _FileWriteFromArray($sFilePath, $aArray, Default, Default, ":|")

But no good.

--------------

Sorry for all these posts but I am learning.

 

; Write array to a file by passing the file name.
_FileWriteFromArray($sFilePath, $pos)

 

This writes the two lines in which I can import them. Sucks because with about12 items we are looking at 24 lines but I'll just have to keep clear records. Still playing..

Edited by Sleepyguy
Link to comment
Share on other sites

If  $sFileread[0] retrieves exactly this  "838,752" you have both cords, and all you need is to split the string. To do that you can use the function StringSplit (this will give you another array), like this:

Local $aArray = StringSplit($sFileread[0], ",", 2)
_ArrayDisplay($aArray)

You can keep the same write method you were using in the first post. This will work.

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