maestro Posted July 29, 2010 Share Posted July 29, 2010 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 More sharing options...
polps Posted July 29, 2010 Share Posted July 29, 2010 Hello, maybe you have to modify your code in this way: $line = FileReadLine($pfile,) $arr = StringSplit($line,",") $xline = $arr[1] $yline = $arr[2] $P1 = $xline; $P2 = $yline; Link to comment Share on other sites More sharing options...
Xenobiologist Posted July 29, 2010 Share Posted July 29, 2010 _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 More sharing options...
maestro Posted July 29, 2010 Author Share Posted July 29, 2010 (edited) 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 Edited July 29, 2010 by maestro Hello, World!... LAME lol Link to comment Share on other sites More sharing options...
maestro Posted July 29, 2010 Author Share Posted July 29, 2010 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 More sharing options...
czardas Posted July 29, 2010 Share Posted July 29, 2010 I have no time to demonstrate this right now, but you might like to check out _FileReadToArray and _FileWriteFromArray in the help file. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
maestro Posted July 29, 2010 Author Share Posted July 29, 2010 Thanks, I will give it a look, and see if I can get it working. Hello, World!... LAME lol Link to comment Share on other sites More sharing options...
maestro Posted July 29, 2010 Author Share Posted July 29, 2010 I must fail at life, I can't seem to get that to work. Hello, World!... LAME lol Link to comment Share on other sites More sharing options...
Authenticity Posted July 29, 2010 Share Posted July 29, 2010 The basics... Unless I'm wrong and the code is indeed correct, the buttonCA function is the only place that locally declare $arr. Thus the array variable is not visible outside of the function if it's not declared there, or at global scope. Link to comment Share on other sites More sharing options...
maestro Posted July 29, 2010 Author Share Posted July 29, 2010 I think I see what your saying, but would that work if I restart the script, And tell it to pull the array from the file I tell it to save to? Hello, World!... LAME lol Link to comment Share on other sites More sharing options...
Authenticity Posted July 29, 2010 Share Posted July 29, 2010 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 More sharing options...
czardas Posted July 29, 2010 Share Posted July 29, 2010 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 operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
maestro Posted July 29, 2010 Author Share Posted July 29, 2010 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 More sharing options...
maestro Posted July 29, 2010 Author Share Posted July 29, 2010 Thanks czardas, I will give that a go. Hello, World!... LAME lol Link to comment Share on other sites More sharing options...
czardas Posted July 29, 2010 Share Posted July 29, 2010 You will not need two arrays. That is just for the demonstration. Good luck with it, gotta go! operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
maestro Posted July 29, 2010 Author Share Posted July 29, 2010 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 More sharing options...
Xenobiologist Posted July 29, 2010 Share Posted July 29, 2010 #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 More sharing options...
maestro Posted July 29, 2010 Author Share Posted July 29, 2010 I will give that a go. Thanks. Hello, World!... LAME lol Link to comment Share on other sites More sharing options...
maestro Posted July 29, 2010 Author Share Posted July 29, 2010 So that works, however, how would I make it save more then just 1 set of x/y info, and then read the proper sets, like first xy, next xy, and so on? Thanks. Hello, World!... LAME lol Link to comment Share on other sites More sharing options...
Xenobiologist Posted July 29, 2010 Share Posted July 29, 2010 #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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now