Jump to content

Parsing Text File To Pull Data Into Variables


 Share

Recommended Posts

Ok, so I am now trying to work with a text file but I am not completely clear on the FileOpen and FileRead functions...

From what I gather I need to run a FileOpen on the file, then I can run a FileRead... Is this correct? Well when I want to do is read the file's entire content. Break it up into the three variables, replace some strings in the set more variables.

How should I approach this? In SQL I would create a temporary table to store my data while I process it but I am not completely clear on how I do that with AutoIt.

Do I use the FileWrite and StringReplace to manipulate the file or can I take the entire file into memory and just manipulate it through variables?

Thanks,

Mark

Link to comment
Share on other sites

  • Moderators

Hi, Murky024. There are a couple of ways to parse the data you're looking for. You are correct that you'll want to do a FileOpen() in order to manipulate the file. Take a look at _FileReadToArray in the Help file. That may give you a starting point.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Hi, Murky024. There are a couple of ways to parse the data you're looking for. You are correct that you'll want to do a FileOpen() in order to manipulate the file. Take a look at _FileReadToArray in the Help file. That may give you a starting point.

Ok, so this leads me to my next series of questions...

If I do a _FileReadToArray can I skip the FileOpen?

Can I _FileReadToArray using a StringSplit?

I am just not clear how _FileReadToArray is going to split the string up...

Thanks for your patiences as I figure this out... I have been Googling up a storm but I can't seem to find examples that completely clear it up for me.

Link to comment
Share on other sites

  • Moderators

Hi, murky024. Regarding your questions:

If I do a _FileReadToArray can I skip the FileOpen?

Try it! :)

Can I _FileReadToArray using a StringSplit?

Yes, you can use a StringSplit. Something like this should get you started:

#include <Array.au3>
#include <File.au3>
$path = "C:error.log"
FileOpen($path, 0)
$sArray = StringSplit(FileRead($path), @LF) ;Removes Carriage Returns
_ArrayDisplay($sArray)

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

So I created a function using some code I found to allow me to pass some some variables into a stringsplit for multiple dimensions at will.

Func _StringSplitToArray($String,$Delimiter,$Flag)
   Dim $Display, $NumCols
;---------------------------------------------------------------------
;Lets convert a comma delimited string into a single dimension array
;---------------------------------------------------------------------
$Columns = StringSplit($String,$Delimiter,$Flag)
$NumCols = $Columns[0]
;MsgBox(0,"Calculate # of Dimensions", "How many: " & $Columns[0])
;---------------------------------------------------------------------
;Lets build a multidimensional Array with enough columns
;---------------------------------------------------------------------
Dim $array[2][ $Columns[0] ]
For $i = 1 To $Columns[0]
    $array[1][$i-1] = $Columns[$i]
Next
;---------------------------------------------------------------------
;Display comma delimited string to a multidimensional Array
;---------------------------------------------------------------------
For $j = 1 To $NumCols
    $Display = $Display & "array[1]["&String($j-1)&"]" & Chr(9) & " = " & chr(9) & $array[1][$j-1] & @CRLF
Next
MsgBox(4096, "Your Multidimensional Array",$Display)
   EndFunc
Link to comment
Share on other sites

Ok, so I have taken that script I found and added some actual multidimensional logic (just 2d). I have 2 seperate dilmiters I look for. I cycle through the 1st fine and I can then get that array into the 2nd StringSplit but the problem after that is I have the StringSplit in a For loop so I end up re-writing the array. I need some way that I can declare a variable with an incrimenting number so at the end of my loop I have one array variable per for loop.

Here is what I have (doesn't work but will be good for the idea)

Dim $lines,$Display,$NumCols,$NumRows
$lines = "Monday,1;Tuesday,2;Wednesday,3;Thursday,4;Friday,5;Saturday,6;Sunday,7"
;---------------------------------------------------------------------
;Lets convert a comma delimited string into a single dimension array
;---------------------------------------------------------------------
$Rows = StringSplit($Lines, ";")
$NumRows = $Rows[0]
;MsgBox(0,"Calculate # of Rows", "How many: " & $Rows[0])
;For $a = 1 To $NumRows + 1
;MsgBox(0,"Row StringSplit Output",$Rows[$a-1])
;Next

;---------------------------------------------------------------------
;Lets build a multidimensional Array with enough columns
;---------------------------------------------------------------------
$ColCount =StringSplit($Rows[1],",",1)
$NumCols = $ColCount[0]
;MsgBox(0,"Calculate # of Columns", "How many: " & $NumCols)
For $b = 1 To $NumRows
Local $Columns[$NumCols] & $b
;MsgBox(0,"Row StringSplit InPut",$Rows[$b])
Local $Columns[$NumCols][$NumRows]
$Columns[ = StringSplit($Rows[$b], ",",1)
MsgBox(0,"Output of StringSplit",$Columns[$b-1])
Next
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...