Alupis Posted January 7, 2011 Share Posted January 7, 2011 (edited) Ok so basically I need to have a small script that will read a .txt file and count how many lines appear with a particular word in them (IGNORING HOW MANY TIMES THAT WORD SHOWS UP PER LINE). So for example: If I had a sample .txt file that had this as the contents: SAMPLE TEXT SAMPLE TEXT SAMPLE TEXT ANOTHER TEXT ANOTHER TEXT SAMPLE ANOTHER TEXT SAMPLE SAMPLE SAMPLE TEXT TEXT TEXT ANOTHER ANOTHER ANOTHER and the script would look through this file for lines with the word "SAMPLE" in it and return = 3 since there are 3 lines that have that word in them... hopefully i'm making sense! lol so far I have this script written: $file = FileOpen("MI.TXT", 0) Dim $pptotal=0 Dim $fcitotal=0 Dim $fctotal=0 Dim $cantotal=0 ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in lines of text until the EOF is reached While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop $array = StringSplit($line, " ") If ($array[10] = "Parcel Post") Then $pptotal = $pptotal + 1 EndIf If ($array[10] = "First-Class International, Parcel") Then $fcitotal = $fcitotal + 1 EndIf If ($array[10] = "First-Class") Then $fctotal = $fctotal + 1 EndIf ####NEED SOMETHING HERE TO COUNT THE LINES WITH THE WORD CANADA IN THEM AND THEN ADD THOSE LINES UP TO BE DISPLAYED BELOW IN THE $cantotal MsgBox########### Wend MsgBox(0, "Total Package Count", "We have shipped "&$pptotal&" Domestic Packages, "&$fcitotal&" First-Class International Packages, "&$fctotal&" First-Class Packages, "&$cantotal&" Canada Packages") FileClose($file) thanks for the help guys... i'm pretty new to this stuff. Edited January 7, 2011 by Alupis Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 7, 2011 Moderators Share Posted January 7, 2011 Alupis,StringInStr is what you need to search for a sub-string within a string. And as many have discovered to their cost, it does ignore the number of times the sub-string appears! 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...
UEZ Posted January 7, 2011 Share Posted January 7, 2011 (edited) Here another alternative: ;~ $text = FileRead("MI.TXT") $text = "SAMPLE TEXT SAMPLE TEXT SAMPLE TEXT" & @LF & _ "ANOTHER TEXT ANOTHER TEXT" & @LF & _ "SAMPLE ANOTHER TEXT" & @LF & _ "SAMPLE SAMPLE SAMPLE" & @LF & _ "TEXT TEXT TEXT" & @LF & _ "ANOTHER ANOTHER ANOTHER" $text &= @LF $search = "sample" $aCount = StringRegExp($text, ".*(?i:" & $search & ").*\n", 3) MsgBox(0, "Information", "Found " & UBound($aCount) & " lines with string " & $search) Br, UEZ Edited January 7, 2011 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Alupis Posted January 10, 2011 Author Share Posted January 10, 2011 ok so I tried the StringInStr way and its not giving me my variable like i'm thinking it should. here's my current code: $file = FileOpen("MI.TXT", 0) Dim $pptotal=0 Dim $fcitotal=0 Dim $fctotal=0 Dim $cantotal=0 ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in lines of text until the EOF is reached While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop $array = StringSplit($line, " ") If ($array[10] = "Parcel Post") Then $pptotal = $pptotal + 1 EndIf If ($array[10] = "First-Class International, Parcel") Then $fcitotal = $fcitotal + 1 EndIf If ($array[10] = "First-Class") Then $fctotal = $fctotal + 1 EndIf $can = StringInStr($array, "CANADA") Wend MsgBox(0, "Total Package Count", "We have shipped "&$pptotal&" Parcel Post, "&$fctotal&" First-Class Packages, "&$fcitotal&" First-Class International Packages, "&$can&" Canada Packages") FileClose($file) The lines $can = StringInStr($array, "CANADA") should search the $array which was defined earlier as $array=StringSplit($line, " ") with $line=FileReadLine($file) and $file=FileOpen("MI.TXT", 0). So if my logic is correct $array should equal my file split into lines correct? and when i StringInStr $array it would then tell it to read each line and look for the word "CANADA" correct? however its not so i have to have something messed up somewhere. My file that i'm working with is a "TAB" seperated .txt file... with the end of each line broken with an "ENTER" key. i hope i'm making sense since i'm still kind of new to this. thanks! Link to comment Share on other sites More sharing options...
nitekram Posted January 10, 2011 Share Posted January 10, 2011 (edited) change this line $can = StringInStr($array, "CANADA") to if StringInStr($array, "CANADA") then $can += 1 Edit - you will also have to declare the var $can Edit - nevermind - you will have to do more than that - as the array is not indexed, and your code needs to do that - i am sure someone will write it faster, but i will try Edited January 10, 2011 by nitekram 2¢ All by me:"Sometimes you have to go back to where you started, to get to where you want to go." "Everybody catches up with everyone, eventually" "As you teach others, you are really teaching yourself." From my dad "Do not worry about yesterday, as the only thing that you can control is tomorrow." WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2 AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit Docs SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language Programming Tips Excel Changes ControlHover.UDF GDI_Plus Draw_On_Screen GDI Basics GDI_More_Basics GDI Rotate GDI Graph GDI CheckExistingItems GDI Trajectory Replace $ghGDIPDll with $__g_hGDIPDll DLL 101? Array via Object GDI Swimlane GDI Plus French 101 Site GDI Examples UEZ GDI Basic Clock GDI Detection Ternary operator Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 10, 2011 Moderators Share Posted January 10, 2011 Alupis,Unless you know which element of the split array will hold the word "CANADA", you need to search the entire line. So you should use something like this:If StringInStr($line, "CANADA") Then $can += 1Of course if you know which element should be "CANADA" (you seem to know what the other elements are) then you just need to add the element index to $array:If StringInStr($array[index], "CANADA") Then $can += 1Note the easy syntax for adding to a variable - much easier on the typing fingers than $can = $can + 1. M23P.S. When you post code please use Code tags. Put [autoit ] before and [/autoit ] after your posted code (but omit the trailing space - it is only there so the tags display here). 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...
Alupis Posted January 10, 2011 Author Share Posted January 10, 2011 Awesome! I went with this code and it seems to be working!: expandcollapse popup$file = FileOpen("MI.TXT", 0) Dim $pptotal=0 Dim $fcitotal=0 Dim $fctotal=0 Dim $cantotal=0 ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in lines of text until the EOF is reached While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop $array = StringSplit($line, " ") If ($array[10] = "Parcel Post") Then $pptotal = $pptotal + 1 EndIf If ($array[10] = "First-Class International, Parcel") Then $fcitotal = $fcitotal + 1 EndIf If ($array[10] = "First-Class") Then $fctotal = $fctotal + 1 EndIf If StringInstr($array[5], "CANADA") Then $cantotal = $cantotal + 1 EndIf $IntTotal = $fcitotal - $cantotal $DomTotal = $pptotal + $fctotal Wend MsgBox(0, "Total Package Count", "We have shipped "&$DomTotal&" Domestic Packages "&$IntTotal&" International Packages, "&$cantotal&" Canada Packages") FileClose($file) Thanks for all the help guys! and quick help too! ;P Link to comment Share on other sites More sharing options...
GEOSoft Posted January 10, 2011 Share Posted January 10, 2011 (edited) This will avoid the array and save you the trouble of looping through the array.$sString = "Canada is my Home" & @CRLF $sString &= "My home is in Canada and Canada is the place for me to be" & @CRLF $sString &= "Canada is this and Canada is that but mainly Canada is just Canada." & @CRLF $sString &= "Australia is a better place for M23, far away from Canada" MsgBox(0, "Result", "The word Canada is found in " & _String_CountLinesUsingWord($sString, "Canada") & " Lines.") Func _String_CountLinesUsingWord($s_Str, $s_Word, $i_Case = 0) If FileExists($s_Str) Then $s_Str = FileRead($s_Str) Local $sCase = "(?i)" If $i_Case <> 0 Then $sCase = "" StringRegExpReplace($s_Str, $sCase & "(?m:^).*\b(" & $s_Word & ")\b.*(?:\v|$)+", "$1") Return @Extended EndFuncEdit: Added word boundary checking to expression and extra parameter for case sensitivity. To make it case-sensitive use anything except 0 (zero) as the last parameter.Edit2: Fixed a screwup in the function. Edited January 10, 2011 by GEOSoft George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
Alupis Posted January 10, 2011 Author Share Posted January 10, 2011 man i love how quick people respond on this forum! thanks for all the help guys but for the moment i think i've got it! lol. i'll post back if something breaks or stops working Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 10, 2011 Moderators Share Posted January 10, 2011 GEOSoft,I am already far enough away from you - do I have to go and live in Oz? We play much better cricket here nowadays - 3-1 in the Ashes! 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...
GEOSoft Posted January 10, 2011 Share Posted January 10, 2011 Ha! Now I have to ponder whether or not you could ever be far enough away from me. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 10, 2011 Moderators Share Posted January 10, 2011 GEOSoft, At present we are about 4700 miles apart on a great circle routing - is that good enough! 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...
GEOSoft Posted January 10, 2011 Share Posted January 10, 2011 For now I suppose. Cricket? Isn't that the poor cousin of that great all-American sport baseball? George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 10, 2011 Moderators Share Posted January 10, 2011 GEOSoft,No, baseball has its origins in rounders, which in its country of invention is played by small girlies at primary school. Cricket is a far more subtle and engaging sport, but one which would probably not appeal to those nationalities who demand "immediate gratification" as a game can last for 5 days and still end in draw. There are more "immediate" versions of the game for those suffering from ADHD, but for real purists Test cricket is the only real way for the game to be played. And against the Aussies is the best of all worlds - especially when we win! 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...
Alupis Posted January 10, 2011 Author Share Posted January 10, 2011 (edited) ok so i guess i'm having a small problem with my code. i just tried having it count a new MI.TXT file that was just generated and its getting everything added up wrong. For some reason it doesn't seem to be counting all of the lines with "CANADA" in them (its coming close but missing 1 or 2) and its also adding up the rest of the data incorrectly... so i must have something off. here's my current code: expandcollapse popup$file = FileOpen("MI.TXT", 0) Dim $pptotal=0 Dim $fcitotal=0 Dim $fctotal=0 Dim $cantotal=0 ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in lines of text until the EOF is reached While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop $array = StringSplit($line, " ") If ($array[10] = "Parcel Post") Then $pptotal = $pptotal + 1 EndIf If ($array[10] = "First-Class International") Then $fcitotal = $fcitotal + 1 EndIf If ($array[10] = "First-Class") Then $fctotal = $fctotal + 1 EndIf If StringInstr($array[5], "CANADA") Then $cantotal = $cantotal + 1 EndIf $IntTotal = $fcitotal - $cantotal $DomTotal = $pptotal + $fctotal ;added the below line to prevent shipping Negative amounts of packages If $IntTotal < 0 Then $IntTotal = 0 Wend MsgBox(0, "Total Package Count", "We have shipped "&$DomTotal&" Domestic Packages "&$IntTotal&" International Packages, "&$cantotal&" Canada Packages") FileClose($file) i'm also uploading a sample MI.TXT file that we use incase i'm missing something file specific. it should add up to 7 Canada Packages, 2 International Packages, and 16 Domestic Packages... however its givign me 8 Canada Packages, 0 International, and 16 Domestic. am i missing something obvious? its almost like its not counting all the lines or actually reading for the words in"" that are defined above such as "CANADA" or "First-Class International" etc...mi.txt Edited January 10, 2011 by Alupis Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 10, 2011 Moderators Share Posted January 10, 2011 Alupis,- 1. You need to use StringInStr to check for "First-Class International" as it is only part of the string "First-Class International, Letter". - 2. I count 8 lines with "CANADA" - 7 with "BUNCH OF ADDRESS STUFF CANADA , CANADA" and 1 with "BUNCH OF ADDRESS STUFF , CANADA". So with this small change:If StringInStr($array[10], "First-Class International") Then $fcitotal = $fcitotal + 1 EndIfI think you are good to go. 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...
UEZ Posted January 10, 2011 Share Posted January 10, 2011 (edited) Based on my previous post: $file = FileRead("mi.txt") & @LF $pptotal = Amount($file, "Parcel Post") $fcitotal = Amount($file, "First-Class International") $fctotal = Amount($file, "First-Class") $cantotal = Amount($file, "CANADA") $IntTotal = $fcitotal - $cantotal $DomTotal = $pptotal + $fctotal If $IntTotal < 0 Then $IntTotal = 0 MsgBox(0, "Total Package Count", "We have shipped "&$DomTotal&" Domestic Packages "&$IntTotal&" International Packages, "&$cantotal&" Canada Packages") Exit Func Amount($string, $search) Local $aCount = StringRegExp($string, ".*(?i:" & $search & ").*\n", 3) Return (UBound($aCount)) EndFunc Br, UEZ Edited January 10, 2011 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Alupis Posted January 10, 2011 Author Share Posted January 10, 2011 ok so i ended up going with Melba23's idea and it worked perfectly! thanks again guys for all your help... and i must say again, WOW you guys are fast! love this forum! 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