superbosu Posted February 21, 2011 Share Posted February 21, 2011 (edited) _FileReadToArray("c:\file.txt",$A) For $x = 1 To $A[0] $An = $A[$x] NextHi all, I hope somebody can help me. this is what I want to do: I have a txt file, and I want to find all words that begin with "GED" and put them on a new txt the words that I want to find are like this : GED124356 the only part of the word that don't change is "GED", the following 6 numbers can change. is there a way to do this thing? I know how to read the file and put it on an array, but I don't know how to extract the word GED569406 out. this can be the part to read the file: Edited February 21, 2011 by superbosu Link to comment Share on other sites More sharing options...
Fran Posted February 21, 2011 Share Posted February 21, 2011 (edited) How about reading all the words into an array and comparing the left 3 characters of your word: if StringLen($word,3)=GED then add to the new text file endif Edited February 21, 2011 by Fran Link to comment Share on other sites More sharing options...
hannes08 Posted February 21, 2011 Share Posted February 21, 2011 (edited) Hello superbosu, please take a look at StringInStr() function in the helpfile, with this you can find the "GED" substring in a string. Edit: Or you could use FileRead to read the whole File into a variable and do a StringSplit($text_of_file, " ") to have an array with each word. ... But on the other hand this may be very ineffective.... regards, Hannes Edited February 21, 2011 by Hannes123 Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler] Link to comment Share on other sites More sharing options...
Fran Posted February 21, 2011 Share Posted February 21, 2011 please take a look at StringInStr() function in the helpfile, with this you can find the "GED" substring in a string.or you can do it the proper way.. hehehe Link to comment Share on other sites More sharing options...
hannes08 Posted February 21, 2011 Share Posted February 21, 2011 Hi Fran, If StringLen($word,3)="GED" Then will never become TRUe, because StringLen() returns an int value. I think you meant StringLeft()? regards, Hannes Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler] Link to comment Share on other sites More sharing options...
Fran Posted February 21, 2011 Share Posted February 21, 2011 I think you meant StringLeft()?It's monday Link to comment Share on other sites More sharing options...
superbosu Posted February 21, 2011 Author Share Posted February 21, 2011 mhhh maybe is quite difficult for me (i'm a newbie) or maybe I didn't understand well.this is an example of the file.txt :"Hi all,how are you? can you import GED342546 on the system?and also do the same thing with GED876453 please?tnxvaleria "I need to put GED342546 and GED876453 on a new_file.txt in this way:GED342546GED876453I'dont understand how to do this Link to comment Share on other sites More sharing options...
saywell Posted February 21, 2011 Share Posted February 21, 2011 This is a job for StringRegExp William Link to comment Share on other sites More sharing options...
hannes08 Posted February 21, 2011 Share Posted February 21, 2011 (edited) Hi saywell, you're right. Read the whole file to a single string variable using FileRead() then run $array = StringRegExp($string,"GED[0-9]{6}",3) Edit: The returning array will contain all matches. Attention this array is 0-based, you'll need UBound() to get all values. Regards, Hannes Edited February 21, 2011 by Hannes123 Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler] Link to comment Share on other sites More sharing options...
superbosu Posted February 21, 2011 Author Share Posted February 21, 2011 #include <GUIConstantsEx.au3> #include <file.au3> $string = FileRead("d:\file.txt") $array = StringRegExp($string,"GED[0-9]{6}",3) for $i = 0 to UBound($array) - 1 msgbox(0, "test " & $i, $array[$i]) Next mhhh... i tryed with this but nothing happens Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 21, 2011 Moderators Share Posted February 21, 2011 superbosu, Try running this and see if you get the 2 GEDs into the array: #include <Array.au3> $sText = "Hi all," & @CRLF & _ "how are you? can you import GED342546 on the system?" & @CRLF & _ "and also do the same thing with GED876453 please?" & @CRLF & _ "tnx" & @CRLF & _ "valeria" $aGEDs = StringRegExp($sText, "GED\d{6}", 3) ; basically the other SRE but with a slightly different syntax _ArrayDisplay($aGEDs) If you do, then we need to investigate why you do not when reading the text from file. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
saywell Posted February 21, 2011 Share Posted February 21, 2011 (edited) You need to open the file before reading it - from the helpfile: $file = FileOpen("test.txt", 0) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in 1 character at a time until the EOF is reached While 1 $chars = FileRead($file, 1) If @error = -1 Then ExitLoop MsgBox(0, "Char read:", $chars) Wend FileClose($file) William [edit] - no I'm wrong. can open with filename. Edited February 21, 2011 by saywell Link to comment Share on other sites More sharing options...
superbosu Posted February 21, 2011 Author Share Posted February 21, 2011 Hi M23 yes it works with your code the array pops up with the two GED.... then I tryed with this but nothing happens again maybe something is missing... #include <Array.au3> $string = FileRead("d:\file.txt") $array = StringRegExp($string, "GED\d{6}", 3) _ArrayDisplay($array) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 21, 2011 Moderators Share Posted February 21, 2011 superbosu, Then let us see if you are reading the file correctly - please run this: $string = FileRead("d:\file.txt") MsgBox(0, "Text", $string) Are you reading the file text correctly? Do the GEDs appear in the text? Do both GEDs have exactly 6 digits? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
superbosu Posted February 21, 2011 Author Share Posted February 21, 2011 DOH!!!!! so stupid... my fault.. now I can see the array! the file is not GED123456 but GEDK123456 sorry, now with this code works tnx a lot! #include <Array.au3> $file = FileOpen("D:\file.txt", 0) $string = FileRead($file) MsgBox(0, "Text", $string) $array = StringRegExp($string, "GEDK\d{6}", 3) _ArrayDisplay($array) FileClose($file) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 21, 2011 Moderators Share Posted February 21, 2011 superbosu, Good. Debugging is a pain - but if you do it logically, you usually get there in the end. Are there any other variations on the GED/GEDK letters? Is it always 6 digits following? We can adjust the SRE if required to fit. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
superbosu Posted February 21, 2011 Author Share Posted February 21, 2011 (edited) ok now the problem is another one... on the new file d:\CR.txt I found only the second GEDK926080 why? what is wrong? #include <Array.au3> $file = FileOpen("D:\file.txt", 0) $string = FileRead($file) MsgBox(0, "Text", $string) $array = StringRegExp($string, "GEDK\d{6}", 3) _ArrayDisplay($array) FileClose($file) for $i = 0 to UBound($array) - 1 msgbox(0, "test " & $i, $array[$i]) $file2 = FileOpen("D:\CR.txt", 2); 2 = Write mode (erase previous contents) FileWriteLine($file2, $array[$i] & @CRLF) FileClose("D:\CR.txt") Next Edited February 21, 2011 by superbosu Link to comment Share on other sites More sharing options...
superbosu Posted February 21, 2011 Author Share Posted February 21, 2011 mhh maybe the 2 = Write mode Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 21, 2011 Moderators Share Posted February 21, 2011 superbosu,You are opening the file for overwriting inside the loop and so deleting the existing file on the second pass. Try it like this: #include <Array.au3> ; You can read a file directly - no need to open $string = FileRead("D:\file.txt") MsgBox(0, "Text", $string) $array = StringRegExp($string, "GEDK\d{6}", 3) _ArrayDisplay($array) ; Open the file $file2 = FileOpen("D:\CR.txt", 2); 2 = Write mode (erase previous contents) ; Start the loop For $i = 0 To UBound($array) - 1 ; Write the lines FileWriteLine($file2, $array[$i] & @CRLF) Next ; Close the file FileClose($file2) ; Note you use the handle, not the file nameI made a few other comments you might like to read as well. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
superbosu Posted February 21, 2011 Author Share Posted February 21, 2011 Many tnx M23 tnx to everybody for your help! appreciated 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