MightyWeird Posted August 11, 2020 Share Posted August 11, 2020 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 More sharing options...
Developers Jos Posted August 11, 2020 Developers Share Posted August 11, 2020 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 More sharing options...
Nine Posted August 11, 2020 Share Posted August 11, 2020 (edited) 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 August 11, 2020 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Developers Jos Posted August 11, 2020 Developers Share Posted August 11, 2020 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 More sharing options...
Nine Posted August 11, 2020 Share Posted August 11, 2020 @Jos Yep true, my bad... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
MightyWeird Posted August 11, 2020 Author Share Posted August 11, 2020 Hi, This gives me something to tinker with. Bedankt / Thank you! Link to comment Share on other sites More sharing options...
jchd Posted August 11, 2020 Share Posted August 11, 2020 Why not faster? #include <Array.au3> $file = @scriptdir & "\resources\test\list.txt" Local $aWords = StringRegExp(FileRead($file), "(?m)^(.*?) .*\R", 3) _ArrayDisplay($aWords) seadoggie01 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
Malkey Posted August 12, 2020 Share Posted August 12, 2020 This script, #include <Debug.au3> $file = @ScriptFullPath Local $aWords = StringRegExp(FileRead($file), "(?m)^\s*([^\s]+)\h*.*\R?", 3) _DebugArrayDisplay($aWords) returns #include $file Local _DebugArrayDisplay($aWords) Link to comment Share on other sites More sharing options...
flux Posted October 16, 2022 Share Posted October 16, 2022 2 years later, just wanted to say thanks for the answers 👍 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now