CyberSlug Posted February 16, 2004 Share Posted February 16, 2004 I have a tex tfile with some number of lines in the following format: FunctionName [tab] SyntaxSpec I'd like to read this file into a two-column array as efficiently as possible. My Ideas: 1) Require user to put the line count at the very top of the text file. 2) Read the once to get the line count, and a second time to fill the array. Any better ideas? Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig! Link to comment Share on other sites More sharing options...
Administrators Jon Posted February 16, 2004 Administrators Share Posted February 16, 2004 I'd count the lines first, as long as the file is not that big it will probably fit into cache so the second read will be quick. Deployment Blog: https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming: https://www.autoitconsulting.com/site/sccm-sdk/ Link to comment Share on other sites More sharing options...
CyberSlug Posted February 16, 2004 Author Share Posted February 16, 2004 Darn. I was hoping you'd say:I saw this post and thought, waa?? We can't even ReDim an array?? How did that happenThanks anyway, Jon Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig! Link to comment Share on other sites More sharing options...
Valik Posted February 16, 2004 Share Posted February 16, 2004 Dim a large amount, create grow() and shrink() functions. If you fill the initial size, grow() it to 1.5 or whatever you feel, then once you've read the entire file, you can use shrink() to bring it back down so you don't waste memory. This will be a fairly expensive operation in memory as both grow() and shrink() will have to copy stuff. But I think that should be faster than doing the file operation twice. grow() - Basically ReDim, except it'll have to work on multi-dimensional arrays, which my ReDim function doesn't. shrink() - Either loop through the array looking for "uninitialized" data to mark the end, or pass it a value to specify the new size. This works like grow(), it copies all elements in the original array till its told to stop. The simple way is obviously to specify the number of lines in the file at the top. Link to comment Share on other sites More sharing options...
Valik Posted February 16, 2004 Share Posted February 16, 2004 Oooh, I forgot about cacheing.... Link to comment Share on other sites More sharing options...
CyberSlug Posted February 16, 2004 Author Share Posted February 16, 2004 I could change the _FileReadToArray to use both @TAB and @LF as delimiters. Reult is array with odd elements as the FunctionName and even elements [except elt 0] as the SyntaxSpecs. Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig! Link to comment Share on other sites More sharing options...
scriptkitty Posted February 17, 2004 Share Posted February 17, 2004 If your file is actually small enough, you can use StringSplit to dimention it ; test how big $x can get $x="" $file = FileOpen("testread.txt", 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop If $x="" Then $x=$line Else $x=$x&"|"&$line EndIf Wend FileClose($file) $lines=StringSplit($x,"|") dim $out[$lines[0]+1][2] For $i=1 To $lines[0] $out[$i][0]=StringLeft($lines[$i], StringInStr($lines[$i], chr(9))-1) $out[$i][1]=StringTrimLeft($lines[$i],StringInStr($lines[$i], chr(9) )) MsgBox(1,"Line "&$i&" of "&$lines[0],$out[$i][0]&@CRLF&$out[$i][1]) Next Other approach: dim $x[10000] $a=0 $file = FileOpen("testread.txt", 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop $a=$a+1 $x[$a]=$line Wend FileClose($file) dim $out[$a+1][2] For $i=1 To $a $out[$i][0]=StringLeft($x[$i], StringInStr($x[$i], chr(9))-1) $out[$i][1]=StringTrimLeft($x[$i],StringInStr($x[$i], chr(9) )) MsgBox(1,"Line "&$i&" of "&$a,$out[$i][0]&@CRLF&$out[$i][1]) Next dim $x[1] Or a quick made redim() I like $x[0] to basically be Ubound($x) dim $x[10000] $a=0 $file = FileOpen("testread.txt", 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop $a=$a+1 $x[$a]=$line Wend FileClose($file) $y=redim($x,$a) MsgBox(1,Ubound($y),Ubound($x)) MsgBox(1,$a,$y[$a]) Func redim($_x,$_amount) Dim $y[$_amount+1] $y[0]=$_amount For $i=1 To $_amount $y[$i]=$_x[$i] Next Return $y EndFunc Anyway I could use a redim(),Grow() and shrink(), if you want to put them in Just a quick example of sloppy things that seem to work. I didn't do the dual array work on the last example. AutoIt3, the MACGYVER Pocket Knife for computers. Link to comment Share on other sites More sharing options...
CyberSlug Posted February 17, 2004 Author Share Posted February 17, 2004 Ah, that's what I was looking for. Thanks, Scriptkitty P.S. You can use @Tab instead of Chr(9) Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig! Link to comment Share on other sites More sharing options...
scriptkitty Posted February 17, 2004 Share Posted February 17, 2004 Old habit from Access programming. I am just getting used to @crlf. AutoIt3, the MACGYVER Pocket Knife for computers. 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