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:
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()>Et voila!
<cfset request.args.putAll(url)>
<cfset request.args.putAll(form)>
Labels: ColdFusion, Java

4 Comments:
Nice one. I was halfway through a collection loop when I found this post!
Adrian
By
Adrian Lynch, At
13 November 2007 11:25
:)
By
Dom, At
13 November 2007 13:12
You seem very keen to avoid built in coldfusion functions :-) How about
StructAppend(struct1, struct2, overwriteFlag)
eg:
structAppend(FORM, URL);
By
Aaron, At
22 December 2007 22:58
lol brilliant - not keen at all, just didn't realise you could do that. Love it!
By
Dom, At
23 December 2007 00:17
Post a Comment
<< Home