• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 730
  • Last Modified:

Count number of times in list

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#">
	<ul>
<cfoutput><li>#i# #ListValueCount(testString, i, "|")#</li>
</cfoutput>
</ul>
</cfloop>

Open in new window

0
perdiemconsulting
Asked:
perdiemconsulting
  • 2
  • 2
1 Solution
 
_agx_Commented:
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">
       <ul>
       <cfloop index="i" list="#testString#" delimiters="|">
      <cfoutput><li>#i# #ListValueCount(testString, i, "|")#</li></cfoutput>
        </ul>
</cfloop>
0
 
perdiemconsultingAuthor Commented:
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.
0
 
_agx_Commented:
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?
0
 
perdiemconsultingAuthor Commented:
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
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now