wizzardking Posted August 5, 2006 Posted August 5, 2006 What I am trying to do is open a text file, sort it alphabetically and write it back to another text file in alpha order. The error I get when I beta compile is $lArray[$a] improper format of array? I get no error and no result when I beta run. Any help I can get would be greatly appreciated! here's what I wrote so far. <------------------------------------ begin code: #include <Array.au3> Dim $lArray = 4000 $file = FileOpen("c:\aubbs\gabbs.dat", 0) ;config file for getting drive letter ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open config file.") Exit EndIf $drive = FileReadLine($file) FileClose($file) ;end get config ;open text file and read into array $file = FileOpen($drive & "\inetpub\data\blognames.txt", 0) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open blognames file.") Exit EndIf $a = -1 While 1 $a = $a + 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop $lArray[$a] = $line Wend FileClose($file) ;end get text file _ArraySort($lArray) ;sort array _ArrayDisplay($lArray, "Class List of Active Window" ) ;display array in textbox (to be removed once working) ;write output file $file = FileOpen($drive & "\inetpub\data\work\alpha.txt",2) For $b = 0 to $a +1 FileWrite($file,$lArray[$b]) FileClose($file) FileCopy($drive & "\inetpub\data\work\alpha1.tmp",$drive & "\inetpub\data\alpha.dat") -------------------> end code I know my programing style is very very old school, but thats where I came from mostly programing in a language called wccode for the old dos based wildcat bbs software and Quickbasic. So please don't judge the style just the syntax `Mitch
Valuater Posted August 5, 2006 Posted August 5, 2006 1 when you are defining an array it would be Dim $lArray[4000] because Dim $lArray = 4000 does not create an array and gives the value of 4000 to the "variable" $IArray 2 using " File Read to Array " should help expandcollapse popup#include <file.au3> #include <array.au3> ; dim the array name Dim $aRecords ; read the drive/data $file = FileOpen("c:\aubbs\gabbs.dat", 0) ;config file for getting drive letter ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open config file.") Exit EndIf $drive = FileReadLine($file) FileClose($file) ; Check if file opened for reading OK $file = FileOpen($drive & "\inetpub\data\blognames.txt", 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open blognames file.") Exit EndIf ; read the file to an array If Not _FileReadToArray($drive & "\inetpub\data\blognames.txt", $aRecords) Then MsgBox(4096, "Error", " Error reading log to Array error:" & @error) Exit EndIf ; sort the array _ArraySort($aRecords) _ArrayDisplay($aRecords, "Array List") ; write the file $file = FileOpen($drive & "\inetpub\data\work\alpha.txt", 2) For $x = 1 To $aRecords[0] MsgBox(0, 'Record:' & $x, $aRecords[$x]) FileWriteLine($file, $aRecords[$x]) Next ; close the file FileClose($file) *********** NOT TESTED 3 Nice try 8)
wizzardking Posted August 5, 2006 Author Posted August 5, 2006 Ok all seems to work nicely for sorting the array. However, writing the array to a file is not working. I noticed the line For $x = 1 To $aRecords[0] however when looking at the _arraydisplay box the first line is blank. I'm assuming from looking at your code and from the help file that the $array[0] should contain a number corresponding to the number of elelments in the array? but in this it reurns zero. So it never writes the file. Do I need to define $array[0] or is this done automaticaly? `Mitch http://www.bytvdemand.com 1 when you are defining an array it would be Dim $lArray[4000] because Dim $lArray = 4000 does not create an array and gives the value of 4000 to the "variable" $IArray 2 using " File Read to Array " should help expandcollapse popup#include <file.au3> #include <array.au3> ; dim the array name Dim $aRecords ; read the drive/data $file = FileOpen("c:\aubbs\gabbs.dat", 0) ;config file for getting drive letter ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open config file.") Exit EndIf $drive = FileReadLine($file) FileClose($file) ; Check if file opened for reading OK $file = FileOpen($drive & "\inetpub\data\blognames.txt", 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open blognames file.") Exit EndIf ; read the file to an array If Not _FileReadToArray($drive & "\inetpub\data\blognames.txt", $aRecords) Then MsgBox(4096, "Error", " Error reading log to Array error:" & @error) Exit EndIf ; sort the array _ArraySort($aRecords) _ArrayDisplay($aRecords, "Array List") ; write the file $file = FileOpen($drive & "\inetpub\data\work\alpha.txt", 2) For $x = 1 To $aRecords[0] MsgBox(0, 'Record:' & $x, $aRecords[$x]) FileWriteLine($file, $aRecords[$x]) Next ; close the file FileClose($file) *********** NOT TESTED 3 Nice try 8)
wizzardking Posted August 5, 2006 Author Posted August 5, 2006 I noticed that [1] actually equals the number of elements for some reason. Ok all seems to work nicely for sorting the array. However, writing the array to a file is not working. I noticed the line For $x = 1 To $aRecords[0] however when looking at the _arraydisplay box the first line is blank. I'm assuming from looking at your code and from the help file that the $array[0] should contain a number corresponding to the number of elelments in the array? but in this it reurns zero. So it never writes the file. Do I need to define $array[0] or is this done automaticaly? `Mitch http://www.bytvdemand.com
wizzardking Posted August 5, 2006 Author Posted August 5, 2006 Ok changing these lines worked perfectly. ; write the file $file = FileOpen($drive & "\inetpub\data\work\alpha.txt", 2) For $x = 2 To $aRecords[1] MsgBox(0, 'Record:' & $x, $aRecords[$x]) FileWriteLine($file, $aRecords[$x]) Next ; close the file FileClose($file) Thankyou for the help. and I learned some new commands too that will help me in other areas as well! You guys are the best!! `Mitch http://www.tvbydemand.com I noticed that [1] actually equals the number of elements for some reason.
Valuater Posted August 5, 2006 Posted August 5, 2006 (edited) While i was having my coffee..... you figured it out thats great! Glad I could help 8) BTW $aRecords[0] should represent the number of elements, not [1] Edited August 5, 2006 by Valuater
randallc Posted August 5, 2006 Posted August 5, 2006 Hi, either of these lines would fix your script properly [in fact both, to be accurate]; _ArraySort($aRecords,0,1) ;($array, $asc, $base)oÝ÷ ÚÊ2¢çhm²í¶»-zW¦z{ZÛaz{¦mêëzf¢ËazƦxCzh²+b¢}(«¢+Ù½ÈÀÌØíàôÄQ¼U ½Õ¹ ÀÌØíI½É̤´Äuse ubound(array)-1, and it does not need the artificially made [0] counter. Best, Randall ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW
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