Jump to content
Macro Express Forums

How to find the number of times a word appears in a string variable.


Markter
 Share

Recommended Posts

I have a chunk of text in a string variable, and i want to know how many times a word appears in the text, and where the last occurrance of that word is. I could do it using Text File Process, but i was hoping there was a quicker, easier way.

Has anybody done this before?

Thanks!

Mark

Link to comment
Share on other sites

I have a chunk of text in a string variable, and i want to know how many times a word appears in the text, and where the last occurrance of that word is. I could do it using Text File Process, but i was hoping there was a quicker, easier way.

Has anybody done this before?

Thanks!

Mark

I do this all the time. I'm constantly counting the occurrences of words, number sequences, you name it. Not just in text files,

but also if I need to find a specific number sequence on a screen (which looks an awful lot like staring at green floating characters

in the Matrix), and need to know how many times that sequence appears on the screen, I use a macro just like this or very similar.

 

Here you go:

 

In the Scripting Editor it looks like this:

Variable Set String %T1% from File: "text counter.txt"
Variable Set String %T2% from Prompt
Variable Set Integer %N1% from Length of Variable %T1%
Variable Set Integer %N2% from Length of Variable %T2%
Replace "%T2%" with "" in %T1%
Variable Set Integer %N3% from Length of Variable %T1%
Variable Modify Integer: %N4% = %N1% - %N3%
Variable Modify Integer: %N5% = %N4% / %N2%
Text Box Display: Final Count

 

The Direct Editor script (copy from here into your macro) looks like this:

<TVAR2:01:04:C:\Users\Steven\Desktop\text counter.txt><TVAR2:02:02:FWhat word needs to be counted?FFCenter:Center><IVAR2:01:12:1><IVAR2:02:12:2><TMVAR2:21:01:01:000:000:%T2%><IVAR2:03:12:1><NMVAR:02:04:1:0000001:1:0000003><NMVAR:04:05:1:0000004:1:0000002><TBOX4:T:1:CenterCenter000278000200:000:Final Count%T2% appears %N5% time(s) in this document.>

 

Of course you may need to make some adjustments for your file location etc. but the math (everything to do with the %N%

variables) is how you count the occurrences of whatever word (or any string of characters - including spaces) you desire.

 

Here's the basic explanation. If the file is 1,000 characters long, and the word you want to count is 5 letters long, you do the

following math: N1 = 1000 (the length of the file). N2 = 5 (the length of the word).

 

Now you replace the word with "" in the file. If the word appears 5 times, then the file gets shorter by 25 characters (5

occurrences of a 5 letter word get replaced with "" (blank) - 5x5=25). In this example we know this, but the macro doesn't know

it yet. So we continue.

 

We set N3 to the length of the file after removing all occurrences of the word. Therefore N3 = 975. The next step is to have the

macro determine the difference between the original length of the file and the new length of the file:

 

N1-N3 = N4 which is 1000-975 = 25

 

N4 = 25

 

Great. Now we divide N4 by the length of the word: N4/N2 = N5 which is the same as stating 25/5 = 5.

 

The word appears 5 times.

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

×
×
  • Create New...