House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

Comparing two arrays or lists

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Joel Watson
03/06/2007 11:50 AM

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!

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Everett, Al \(NIH/NIGMS\) [C]
03/06/2007 11:57 AM

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.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Joel Watson
03/06/2007 12:10 PM

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 -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Ben Nadel
03/06/2007 12:01 PM

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!

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Joel Watson
03/06/2007 12:10 PM

I am not worthy!  That is awesome!  Thanks! ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andy Matthews
03/06/2007 12:55 PM

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!


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

July 28, 2014

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
     1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31     

Designer, Developer and mobile workflow conference