Friday, 10 February 2012

Equate arrays, structs, query or possible combination of three


Have you ever felt need to compare two arrays in ColdFusion or comparing two structs or may be two queries. If you are reading this, I am sure you felt at some point and if not, you can still read on :)

We use arrays, structs and queries so regularly in ColdFusion that at some point or the other we have to compare them and we dump it to check if its coming fine. Dump is such a powerful tag in ColdFusion that you should use it but what if you want to write a script to check that. Ahhh... cfdump cannot be used there right.

Well not to worry, I have written a code, in fact to be precise I have modified a script (courtesy-cflib.org) which will allow you to compare arrays or structs or queries. Even if you have complex objects like array of struct or array inside struct or query in array of struct or whatever possiblity you can think of these three, you can easily compare using three simple functions: arrayCompare(leftarray, rightarray), structCompare(leftStruct, rightStruct), queryCompare(leftQuery, rightQuery).

I will post complete code n the end, you can save that in .cfm file and call the functions at your will. So it's like if you have two comparable variabes:array of structs, you just need to pass both of them to arrayCompare() OR if you have complex structs having array or query inside you need to call arrayStruct. Handling of array, struct or query present inside your variable will be automatically taken care of.

If both the variables passed by you are equal then this will return a boolean true else false. Find comments inline to understand the code.

So go ahead, you this file, call functions and compare your complex variables at ease.

Click below given link to see the code:



This code is also available at cflib.org but I have modified it according to the need, so it might not be the same. If you are looking for a complete set of three features(array, struct, query compare) this file is perfect for you.

Share if you feel its worth it. Comments will be a +1.

-Milan Chandna

5 comments:

  1. I'm curious why you don't just use equals to test if the objects are the same? All ColdFusion types are Java objects so this will work no matter how complex the object is. If you're concerned about using an underlying (undocumented) feature you can easily wrap it in a UDF just the same so that you can easily fix the issue if it breaks in a future release.

    ReplyDelete
  2. Hi Todd,

    First of all thanks for posting your query.

    Well there are few cases when java.lang.Object.equals(obj1,obj2) will fail in ColdFusion data types. Actually I have not dig this that why does it fail but for now I can quickly give you one example.


    <!--- consider this code --->
    <!--- created an array with a date object inside --->
    <cfset arr1 = ArrayNew(1)>
    <cfset dt1 = CreateDate(2011, 07, 20)>
    <cfset arr1[1] = dt1>

    <!--- dump array --->
    <cfoutput>Array 1:</cfoutput>
    <cfdump var="#arr1#">

    <!--- created another array having formated/masked date object inside --->
    <cfset arr2 = ArrayNew(1)>
    <cfset dt2 = DateFormat(dt1,"mmmm, dd, yyyy")>
    <cfset arr2[1] = dt2>


    <!--- dump array --->
    <cfoutput>Array 2:</cfoutput>
    <cfdump var="#arr2#">

    <!--- checking if dt2 is still a date object of ColdFusion --->
    <cfoutput><br>Is dt2 a date object:</cfoutput>
    <cfoutput>#isdate(dt2)#</cfoutput>

    <!--- calling arrayCompare() function present in complexCompare.cfm given above --->
    <cfoutput><br>Are 2 arrays same(using complexCompare.cfm):</cfoutput>
    <cfoutput>#arrayCompare(arr1,arr2)#</cfoutput>
    <!--- calling equals() function present in java --->
    <cfoutput><br>Are 2 arrays same(using equals function of JAVA):</cfoutput>
    <cfoutput>#arr1.equals(arr2)#</cfoutput>


    <!--- output coming in browser is --->
    <!---
    Array 1:
    array
    1 {ts &apos;2011-07-20 00:00:00&apos;}
    Array 2:
    array
    1 July, 20, 2011

    Is dt2 a date object: YES
    Are 2 arrays same(using complexCompare.cfm): true
    Are 2 arrays same(using equals function of JAVA): NO
    --->

    When I formatted a date object which will still be ColdFusion date object, JAVA equals fails in comparing them but arrayCompare() does it successfully.

    This is just one example, there could be many more.
    BTW thanks to your post, I almost forgot to blog this point(using JAVA equals function in simple cases). I will update the blog soon. But ideally I will recommend to use functions present in that file.

    Thanks,
    Milan.

    ReplyDelete
  3. Nice post Milan..Thanks for sharing :)

    ReplyDelete