Jump to content

Parcing files


Recommended Posts

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

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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

Link to comment
Share on other sites

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.
Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

:D

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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.”

 

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