|
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.
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>
There are no comments for this entry.
[Add Comment] [Subscribe to Comments]