Sign in to follow this  
Followers 0
tsukun03

Parcing files

8 posts in this topic

Hey All I am new to Autoit and I need some help parsing a file word by word. I can't seem to figure out how to use the FileReadLine together with whit stringsplit.

Here some of my code as far as trying to get this done.

$data = FileRead($file)

;_FileReadToArray($file, $aArray)

$d = _FileCountLines($file)

$array = FileReadLine($file)

MsgBox(0, "",$d)

For $i = 1 to $d

$array = FileReadLine($file , $i)

MsgBox(0, "", $array)

$line = StringSplit( $array, "")

;$split = StringSplit($line,"(\s+|\t+)")

MsgBox(0, $i, $line) ; this output is empty. I am not sure what is going on here.

Next

Is the output of FileReadLine a string?

Thanks for you help

Share this post


Link to post
Share on other sites



Welcome to AutoIt and the forum!

To read a line from a file and then parse it into single words (I assume in this example that words are separated by spaces) I would use:

$aLine = FileReadLine($sFile)
$aWords = StringSplit($aLine, " ")

If you need to use more than one separator character you can use something like:

$aWords = StringSplit($aLine, " " & @Tab)
This will split at space and tab characters.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I tried that already and when I run the program in gives me white space when for each word.

The FileReadLine gives the full line of the txt file but when I try and split that line by spaces nothing is in the msgbox just white space

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

here is how you would use your regexp:

$atest = StringRegExp ( $line,"(w+)[s|t]?", 3 )

The String split returns an array, to view it, include this:

#include <Array.au3>
_ArrayDisplay ($atest)

FileReadLine

Read in a line of text from a previously opened text file.

FileReadLine ( "filehandle/filename" [, line] )

Parameters

filehandle/filename The handle of a file, as returned by a previous call to FileOpen. Alternatively you may use a string filename as the first parameter. line [optional] The line number to read. The first line of a text file is line 1 (not zero), last line is -1.

Return Value

Success: Returns a line of text. Special: Sets @error to -1 if end-of-file is reached. Failure: Sets @error to 1 if file not opened in read mode or other error.

Remarks

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

You can't use MsgBox with an array (function StringSplit returns an array). Use something like:

#include <array.au3>
$aLine = FileReadLine($sFile)
$aWords = StringSplit($aLine, " ")
_ArrayDisplqy($aWords)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Ok cool thanks alot this helped a lot

Share this post


Link to post
Share on other sites
:D

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

You can't use MsgBox with an array (function StringSplit returns an array). Use something like:

#include <array.au3>
$aLine = FileReadLine($sFile)
$aWords = StringSplit($aLine, " ")
_ArrayDisplqy($aWords)

If you really really really need someone to press a button to make it do something afterwards, you can parse it into a string then use msgbox() :P

$aLine = FileReadLine($sFile)
$aWords = StringSplit($aLine," ")
$sArrayOutput = ""
For $i = 1 To $aWords[0]
    $sArrayOutput&= $aWords[$i] & @CRLF
Next
msgbox(0,"",$sArrayOutput)

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0