Count number of times in list

Posted on 2012-08-29
Last Modified: 2012-08-29
I have a list with several duplicates.  I want to determine how many times a specific string appears in a list.  I am doing this is a cfloop, but cannot get the correct number of items in the UL / IL I am creating. "Bob" is coming up correctly but none of the other duplicates are.  Here is the code.

<cfset testString = "Bob|James|Fred|Betty|George|Fred|Joe|Jean|Jeff|Alan|Jeff|Joe|Bob|Fred|Alicia|Allison|Taylor|Riley|Scott|Riley|Taylor|Bob|James">
<cfloop index="i" list="#testString#">
<cfoutput><li>#i# #ListValueCount(testString, i, "|")#</li>

Open in new window

Question by:perdiemconsulting
    LVL 51

    Accepted Solution

    You just forgot the delimiter for the loop. Note, ListValueCount is case sensitive! To ignore case use ListValueCountNoCase instead.

    <cfset testString = "Bob|James|Fred|Betty|George|Fred|Joe|Jean|Jeff|Alan|Jeff|Joe|Bob|Fred|Alicia|Allison|Taylor|Riley|Scott|Riley|Taylor|Bob|James">
           <cfloop index="i" list="#testString#" delimiters="|">
          <cfoutput><li>#i# #ListValueCount(testString, i, "|")#</li></cfoutput>

    Author Comment

    When I include the delimiter for the loop, it creates one list item with all the list items, with a 0 on the end for Bob. When I exclude the delimiter for the loop, it creates an unordered list with the list items.
    LVL 51

    Expert Comment

    With the delimiter the code above produces this (CF10):

         * Bob 3
         * James 2
         * Fred 3
         * Betty 1

    If that's incorrect, can you give an example of the correct results?

    Author Comment

    Thanks for the reply. In looking over the cod, I noticed a missing space in a previous loop, which was the issue. Thanks for your help

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Title # Comments Views Activity
    Database structure 4 106
    C# script to Coldfusion 25 43
    how to open dropdown menu on page load without having to click? 4 37
    paging 3 14
    Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
    Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now