Jump to content
Macro Express Forums

Searching A Text File For # Of Times A Word Occurs


JakeARoonie

Recommended Posts

Hello, got another question that I'm sure has another equally obvious answer: I've gotten the word-matching thing going so that my boss can trigger a macro by IMing me. I've gotten the macro to gather the data she requests and save it all to 2 separate text files. Now, what I need to do is count the number of times the word "ASSIGNED" appears in one of the files, the number of times "DISPATCH" occurs in both, and the number of times "NEED TECH INFO" occurs in both. Obviously I'll also need to store the 3 numbers to variables so I can then IM her back with the results, but that should be comparatively simple.

 

The farthest I've gotten with that is that I picked the command "open text file for processing." Not sure what to do from there. Help? :)

Link to comment
Share on other sites

To count the number of occurrences is simple, see my example. There are many ways and sometimes different ways might serve your purpose better but this is one to think about.

 

Basically it’s a repeat that consumes one instance of your text every time in a temporary variable. If one gets the position of text that does not exist in a var you get zero and I use this as the control for how many times it repeats. Basically it keeps repeating until N1=0.

 

Variable Set String %T1% from File: "test.txt"
Repeat Until %N1% = 0
 Variable Modify Integer: Inc (%N2%)
 Variable Set Integer %N1% from Position of Text in Variable %T1%
 Replace "DISPATCH" with "" in %T1%
Repeat End
Variable Modify Integer: Dec (%N1%)
Text Box Display: Result

 

<TVAR2:01:04:c:\test.txt><REP3:08:000001:000002:0001:0:01:0><NMVAR:08:02:0:0000001:0:0000000><IVAR2:01:13:1:DISPATCH><TMVAR2:21:01:00:000:000:DISPATCH><ENDREP><NMVAR:09:01:0:0000001:0:0000000><TBOX4:T:1:CenterCenter000278000200:000:ResultDISPATCH appears %N1% times>

Link to comment
Share on other sites

Cory, should'nt it be like this?

 

<TVAR2:01:04:c:\test.txt><REP3:08:000001:000002:0001:0:01:0><NMVAR:08:02:0:0000001:0:0000000><IVAR2:01:13:1:DISPATCH><TMVAR2:21:01:00:000:000:DISPATCH><ENDREP><NMVAR:09:02:0:0000001:0:0000000><TBOX4:T:1:CenterCenter000278000200:000:ResultDISPATCH appears %N2% times>
Link to comment
Share on other sites

Sorry, once more...

 

<TVAR2:01:04:c:\test.txt><REP3:08:000001:000002:0001:0:01:0><NMVAR:08:02:0:0000001:0:0000000><IVAR2:01:13:1:DISPATCH><TMVAR2:21:01:00:000:000:DISPATCH><ENDREP><NMVAR:09:02:0:0000001:0:0000000><TBOX4:T:1:CenterCenter000278000200:000:ResultDISPATCH appears %N2% times>

Link to comment
Share on other sites

The only difference I see in your text was that you decremented N1 after the repeat loop, I’ll assume this is your only change.

 

You would not decrement N1, N1 is used to find the position of the text. N2 is counting the number of repeats which is what you want to know. But they way I have it you will always get one to many N2s so you need to decrement. For instance if it wasn’t found at all N2 would be set to 1 in the first instance of the repeat. But it should be zero so we decrement.

Link to comment
Share on other sites

In you're macro you decremented N1 after the repeat loop and you showed the result of N1 (DISPATCH). You wrote the code like this:

 

<TVAR2:01:04:c:\test.txt><REP3:08:000001:000002:0001:0:01:0><NMVAR:08:02:0:0000001:0:0000000><IVAR2:01:13:1:DISPATCH><TMVAR2:21:01:00:000:000:DISPATCH><ENDREP><NMVAR:09:01:0:0000001:0:0000000><TBOX4:T:1:CenterCenter000278000200:000:ResultDISPATCH appears %N1% times>

 

Instead would'nt it be decremented N2 after the repeat loop and the result of N2 (Number of DISPATCH). The code like at this:

 

<TVAR2:01:04:c:\test.txt><REP3:08:000001:000002:0001:0:01:0><NMVAR:08:02:0:0000001:0:0000000><IVAR2:01:13:1:DISPATCH><TMVAR2:21:01:00:000:000:DISPATCH><ENDREP><NMVAR:09:02:0:0000001:0:0000000><TBOX4:T:1:CenterCenter000278000200:000:ResultDISPATCH appears %N2% times>

 

Just from my simple point of view. Friday and the sun is shining.

Link to comment
Share on other sites

Oh yeah, I got it confused. I didn't read your code but I looked at mine again and it should have N2 instead of N1 in the last two commands. What I get for not actually trying it.

Link to comment
Share on other sites

Cory: I assumed it was just a simple mistake. I just wanted to point out the problem, just to help JakeARoonie, if he didn't see the problem.

 

(Soon to be celebrating the Swedish midsummer, i say skål! (cheers!) to everyone). :)

Link to comment
Share on other sites

Yes, and I appreciate it.

 

Skål to you too. What will you be drinking tonight I wonder? Personally my favorite is Absolut so I'll use you as an excuse ot have a drink this evening. :D

Link to comment
Share on other sites

To Cory or for that matter anyone:

Excuse me this is quit off the topic but what a heck, It's just a couple of hours now and i'm off to celebrate a swedish holiday (midsummer) tomorrow and want to be polite and answer Corys question :-). The typical food at the middle of the midsummersday is herring with potatoes, sallad and bread, to all this you drink beer and "nubbe" (snaps) i prefer Aquavit (Skåne (Swedish) or Aalborg (Danish)) and then you start off drinking all night long. :-)

Look at this: http://en.wikipedia.org/wiki/Midsummer and Cory it's ok, you can use me as an excuse to drink ones more :-).

 

Oh, as i'm already way out off this forums topic i would recomend this link http://www.slayradio.org/mastering_swedish_lesson_3.php, if anyone wants to learn swedish, even if you don't, it's really funny.

 

Sorry Joe, i'm gonna sneak out of the forum now with my tail between my legs...

 

Skål!

Link to comment
Share on other sites

Sorry I’ve been away for a while. As much as I like computers I also like building things, doing woodwork and the like and I’ve had some fun summer projects.

 

Midsummer sounds like a good time. Near where I grew up in Montana there were some concentrations of Norwegians and Swedes that settled there in the late 19th century so I know of some of these items. However I could never quite get a taste for lutefisk.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...