Saturday, 22 September 2007

Singular URL and FORM variable

After reading this discussion on IsDefined() vs StructKeyExists(), I got to thinking about how one might use the underlying Java methods of ColdFusion structures to combine the URL and FORM variables efficiently (it is related, really).

A quick inspection of the java docs for java.util.Hashtable turned up the putAll() method. With it, you can 'put' one struct into another, overwriting any matching keys.

So, to very quickly and elegantly combine the form and url scopes into a request variable called 'args', we can do:
<cfset request.args = StructNew()>
<cfset request.args.putAll(url)>
<cfset request.args.putAll(form)>
Et voila!

4 comments:

Adrian Lynch said...

Nice one. I was halfway through a collection loop when I found this post!

Adrian

Dom said...

:)

Aaron said...

You seem very keen to avoid built in coldfusion functions :-) How about
StructAppend(struct1, struct2, overwriteFlag)

eg:
structAppend(FORM, URL);

Dom said...

lol brilliant - not keen at all, just didn't realise you could do that. Love it!