|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Comparing two arrays or lists
I have two lists that I want to compare and be able to show the resulting differences. For example, if my first list has 1,2,3,4 and the second has 1,2,4,4, I want to be able to output that "3" and "4" are different.Joel Watson 03/06/07 11:50 A Did you look at ListCompare ( http://www.cflib.org/udf.cfm?ID=149 ) ?Everett, Al \(NIH/NIGMS\) [C] 03/06/07 11:57 A Huh, for some reason I didn't see those when I was looking around cflib before. They all seem to do the trick--thanks!Joel Watson 03/06/07 12:10 P What about something like this:Ben Nadel 03/06/07 12:01 P I am not worthy! That is awesome! Thanks!Joel Watson 03/06/07 12:10 P Take a look at this post by Rupesh Kumar:Andy Matthews 03/06/07 12:55 P I have two lists that I want to compare and be able to show the resulting differences. For example, if my first list has 1,2,3,4 and the second has 1,2,4,4, I want to be able to output that "3" and "4" are different. What would be the best way of going about this? I applied the "arrayCompare" UDF from cflib, but it only outputs a bollean for the entire comparison. I would like to be able to compare on a more detailed level and output the results. Thanks! Did you look at ListCompare ( http://www.cflib.org/udf.cfm?ID=149 ) ? ListDiff ( http://www.cflib.org/udf.cfm?ID=660 ) ? listsAreDistinct ( http://www.cflib.org/udf.cfm?ID=960 ) ? One of those sounds like it'll do what you want. I have two lists that I want to compare and be able to show the resulting differences. For example, if my first list has 1,2,3,4 and the second has 1,2,4,4, I want to be able to output that "3" and "4" are different. What would be the best way of going about this? I applied the "arrayCompare" UDF from cflib, but it only outputs a bollean for the entire comparison. I would like to be able to compare on a more detailed level and output the results. Huh, for some reason I didn't see those when I was looking around cflib before. They all seem to do the trick--thanks! joel ----- Excess quoted text cut - see Original Post for more ----- What about something like this: <cffunction name="GetDiff" access="public" returntype="string" output="false" hint="Gets the difference between two lists based on index."> <!--- Define arguments. ---> <cfargument name="ListOne" type="string" required="true" /> <cfargument name="ListTwo" type="string" required="true" /> <!--- Define the local scope. ---> <cfset var LOCAL = StructNew() /> <!--- Get the length of both lists. ---> <cfset LOCAL.ListOneLength = ListLen( ARGUMENTS.ListOne ) /> <cfset LOCAL.ListTwoLength = ListLen( ARGUMENTS.ListTwo ) /> <!--- Set empty diff list. ---> <cfset LOCAL.DiffList = "" /> <!--- Loop over both lists. ---> <cfloop index="LOCAL.Index" from="1" to="#Max( LOCAL.ListOneLength, LOCAL.ListTwoLength )#" step="1"> <!--- Check to see if we have an element in list one to check. ---> <cfif (LOCAL.Index GT LOCAL.ListOneLength)> <cfset LOCAL.DiffList = ListAppend( LOCAL.DiffList, ListGetAt( ARGUMENTS.ListTwo, LOCAL.Index ) ) /> <cfelseif (LOCAL.Index GT LOCAL.ListTwoLength)> <cfset LOCAL.DiffList = ListAppend( LOCAL.DiffList, ListGetAt( ARGUMENTS.ListOne, LOCAL.Index ) ) /> <cfelseif ( ListGetAt( ARGUMENTS.ListOne, LOCAL.Index ) NEQ ListGetAt( ARGUMENTS.ListTwo, LOCAL.Index ) )> <cfset LOCAL.DiffList = ListAppend( LOCAL.DiffList, ListGetAt( ARGUMENTS.ListTwo, LOCAL.Index ) ) /> </cfif> </cfloop> <!--- Return the diff list. ---> <cfreturn LOCAL.DiffList /> </cffunction> #GetDiff( "1,2,4", "1,2,3,4" )# ..................... Ben Nadel Certified Advanced ColdFusion MX7 Developer www.bennadel.com Need ColdFusion Help? www.bennadel.com/ask-ben/ I have two lists that I want to compare and be able to show the resulting differences. For example, if my first list has 1,2,3,4 and the second has 1,2,4,4, I want to be able to output that "3" and "4" are different. What would be the best way of going about this? I applied the "arrayCompare" UDF from cflib, but it only outputs a bollean for the entire comparison. I would like to be able to compare on a more detailed level and output the results. Thanks! I am not worthy! That is awesome! Thanks! ----- Excess quoted text cut - see Original Post for more ----- Take a look at this post by Rupesh Kumar: http://coldfused.blogspot.com/2007/01/extend-cf-native-objects-harnessing.ht ml It might have what you need. I have two lists that I want to compare and be able to show the resulting differences. For example, if my first list has 1,2,3,4 and the second has 1,2,4,4, I want to be able to output that "3" and "4" are different. What would be the best way of going about this? I applied the "arrayCompare" UDF from cflib, but it only outputs a bollean for the entire comparison. I would like to be able to compare on a more detailed level and output the results. Thanks!
|
May 22, 2013
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||