Jump to content

text file to array, only the first word of every line


Recommended Posts

As the title says, I need a text file in a array, but only the first word of every line. Space is the delimiter.

example content of text file:

thisword1 test3 test3  test3 testtsjflsjdfljflsjf exampletext

iwantthiswordtoo test3 test3  test3 testtsjflsjdfljflsjf exampletext

 

So I want only the first words "thisword" and  "iwantthiswordtoo"

 

I tried a lot of examples, but I just cant figure it how to do this in a array.

Below one of my test and trials...

#include <File.au3>
#include <Array.au3>

$file = @scriptdir & "\resources\test\list.txt"
FileOpen($file, 0)
Global $arr[1000]
ReDim $arr[_FileCountLines($file)+1]

For $i = 1 to _FileCountLines($file)
    $line = FileReadLine($file, $i)
    $arr[$i] = $line
   $arr = stringsplit($file," ")
;~ return $arr[1]
Next

_ArrayDisplay($arr)

 

Link to comment
Share on other sites

  • Developers

something like this?:

#include <File.au3>
#include <Array.au3>

$file = @scriptdir & "\resources\test\list.txt"
$fh = FileOpen($file, 0)
$nrec = _FileCountLines($file)
Dim $arr[$nrec+1]
For $i = 1 to $nrec
    $line = FileReadLine($fh)
    $arr2 = stringsplit($line," ")
    $arr[$i] = $arr2[1]
Next

_ArrayDisplay($arr)

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Don't forget to close the file with FileClose ($fh).

And you need to take into account the fact that the line may contain only one word...

In your example, the 1 of the first word of the first line, does not appear in the result

Edited by Nine
Link to comment
Share on other sites

  • Developers
Just now, Nine said:

Don't forget to close the file with FileClose ($fh).

Agree ... but Autoit3 will handle it as well. 

1 minute ago, Nine said:

And you need to take into account the fact that the line may contain only one word...

Which still works ...no?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Why not faster?

#include <Array.au3>

$file = @scriptdir & "\resources\test\list.txt"
Local $aWords = StringRegExp(FileRead($file), "(?m)^(.*?) .*\R", 3)
_ArrayDisplay($aWords)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • 2 years later...

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