Jump to content

Txt File To 2-column Array


 Share

Recommended Posts

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

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

If your file is actually small enough, you can use StringSplit to dimention it :whistle:

; 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 B)

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

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