views:

262

answers:

1

Hi guys, I am attempting to parse text from a <cfoutput query="...">. I am interested in finding the number of times every word in the text is displayed. For example:

"My name is Bob and I like to Bob".

should result in

Bob - 2
Name - 1
etc, etc, etc.

I take my <cfoutput> from a twitter RSS feed. Here is my code:

<blink>
  <cfset feedurl="http://twitter.com/statuses/user_timeline/47847839.rss" />
  <cftry>
    <cffeed source="#feedurl#" properties="feedmeta" query="feeditems" />
    <cfcatch></cfcatch> 
  </cftry> 
  <ol>
    <cfoutput query="feeditems">
      #content# #id# <br><br>
    </cfoutput>
  </ol>
</blink>

I output a pretty great ordered list, but I can't figure out for the life of me how to parse the content and list how many times each word is used.

Thanks for any help you can provide, I am new to these forums!

+4  A: 

You can find a solution here:

http://www.coldfusionjedi.com/index.cfm/2007/8/2/Counting-Word-Instances-in-a-String

Basically, split the string up using regex and then loop over the results. There are some darn good comments here as well.

CF Jedi Master
something I've noticed lately is that people post answers that link to a blog post or an article somewhere. Over time links tend to break. It would be better to just post the content of the answer here so people searching for this problem in the future can find the solution without being disappointed by clicking on broken links.
Jayson
Good point. Next time, unless the answer is real long, I'll put both a link and the text. Of course, if it means a lot of formatting, it may not be worth the double duty entry.
CF Jedi Master