Shaun Mccran

My digital playground

17
J
U
N
2009

Removing duplicate list items

I was looking to remove duplicate items from a list, a valueList() from a query in fact, and ended out hitting upon the idea to use a struct. ColdFusion structures cannot have duplicate keys, so if I create my list as a struct, it will effectively over right any duplicate values.

Just to clean it up on the other end I extract all the keys back out the structure, giving me a cleaned list.

view plain print about
1<cfset variables.tmpList = '1,1,2,4,4,4,5,6,7,7,9'>
2
3Original: <cfoutput>variables.tmpList = #variables.tmpList#</cfoutput>
4
5<cfset variables.setter = StructNew()>
6<cfloop index="elem" list="#variables.tmpList#">
7    <cfset variables.setter[elem] = "">
8
9<cfoutput>
10variables.setter[#elem#]<br/>
11</cfoutput>
12
13</cfloop>
14<!--- Convert the set back to a list --->
15<cfset variables.tmpList = StructKeyList(variables.setter)>
16
17Filtered:<cfoutput>variables.tmpList = #variables.tmpList#</cfoutput>

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top