Utility Function: StructMerge

If all you need to do is merge two simple structs, then StructAppend() is a great function to get you there, but if you have complex structs (meaning structs that contain structs) you may be wanting to find a solution that does a deep merge:

<cffunction name="StructMerge" access="public" output="false" returntype="struct">
    <cfargument name="Struct1" type="struct" required="true" />
    <cfargument name="Struct2" type="struct" required="true" />
    <cfargument name="Overwrite" type="boolean" required="false" default="true" />
    
    <!--- Loop Keys --->
    <cfloop collection="#Arguments.Struct2#" item="Local.Key">
        <!--- Find if the new key from Struct2 Exists in Struct1 --->
        <cfif StructKeyExists(Arguments.Struct1, Local.Key)>
            <!--- If they are both structs, we need to merge those structs, too --->
            <cfif IsStruct(Arguments.Struct1[Local.Key]) AND IsStruct(Arguments.Struct2[Local.Key])>
                <!--- Recursively call StructMerge to merge those structs --->
                <cfset StructMerge(Arguments.Struct1[Local.Key], Arguments.Struct2[Local.Key], Arguments.Overwrite) />
            <!--- We already checked that the key existed, now we just check if we can overwrite it --->
            <cfelseif Arguments.Overwrite>
                <cfset Arguments.Struct1[Local.Key] = Arguments.Struct2[Local.Key] />
            <!--- The unused case here is if Overwrite is false, in which case Struct1 is not changed --->
            </cfif>
        <!--- If it doesn't exist, you're free to merge --->
        <cfelse>
            <cfset Arguments.Struct1[Local.Key] = Arguments.Struct2[Local.Key] />
        </cfif>
    </cfloop>
    
    <cfreturn Arguments.Struct1 />
</cffunction>

This code merges all sub structures as well as the core one. I'd also say it would be an easy adjustment to merge arrays and other data types if necessary. I'm not sure if CF10 will be adding something like this or not, but I know for CF9.0.1 I needed it.

 


Utility Functions: Two UDFs to Make Spreadsheets Easier

I started diving into using ColdFusion's capabilities to work with Excel spreadsheets for the first time today. Even though its been a part of ColdFusion since CF9 dropped in 2009, I've not been called on to use the functionality, so I'm a bit behind the curve. Once I got moving though, I found some annoyances and gotcha's that drove me to come up with a couple of new UDFs to ease my pain. Find SpreadsheetExpandedInfo() and SpreadsheetGetData() after the jump.

 

Utility Functions: Geometry Functions

Today we've got a double header of functions dealing with Geometry. As I said in my last post, I've been spurred into action by a user's question, and in order to solve his problem, I'm going to need to be able to calculate some relatively simple geometry. Click "more" to see how to convert Degrees to Radians and how to calculate the position of a point based on a starting location, angle, and distance.

 

Utility Function: RoundToClosest()

Its been a while! I got an email from a reader who wanted some help with a bit of image manipulation that has roused me back into CF coding! The particular bit of functionality he wants is rather complex, and so its been forcing me to make a bunch of utility functions to piece it together. First thing I ran into: rounding a number in CF is a little tricky. If you want to round to 2 decimal places, you should multiply the number by 100, call Round() and then divide by 100 again. What a waste. Luckily a little trickery with NumberFormat() can get us what we want.

 

Utility Functions: REExtractAll() and REExtractAllNoCase()

Since it had been so long since I released utility functions, I'm doing another double header today! Not that its really that hard since these two are exactly the same... but any way, continuing where yesterday's REFindAll() and REFindAllNoCase() left off, these two new functions actually return the matching values!

 

Utility Functions: REFindAll() and REFindAllNoCase()

Its been a while since I released a utility function, so today I bring you a very closely related pair that solve a very simple problem thats missing with ColdFusion's Regular Expression functions. Given a regular expression in ColdFusion, you can find out if there is a match for that regular expression in a given string, and where it is, but you can't find out the location of ALL of the values that match the regular expression.

 

More Entries

Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web.

Learn more about me on LinkedIn.