ColdFusion Puzzler - Inspect It!
Today's ColdFusion Puzzler is based on a cool Groovy feature. I was surprised to discover that Groovy supports a Dump function. While I don't find it as pretty as ColdFusion's version, it's nice to have when debugging. But Groovy takes it a bit further and adds something similar called the inspect() function. The inspect function will take any arbitrary object and return a string that could be used to create it. Here is an example:
def s = [ name:"Raymond", age:35, rank:"Jedi" ]
def a = [0,2,3] def b = new Date()
s.a = a s.bornondate = b
println s.inspect()
This returns:
["name":"Raymond", "age":35, "rank":"Jedi", "a":[0, 2, 3], "bornondate":Fri Sep 12 08:48:16 CDT 2008]
As you can see, it isn't the code I used but code that would generate the same data.
Your challege, should you choose to accept it, is to write a similar function for ColdFusion. Your output need not look the exact same of course. I've provided a simple example that only works with arrays to get your started.
<cfscript> function inspect(arr) { var r = ""; var i = "";
r = "["; for(i=1; i <= arrayLen(arr); i++) { r &= arr[i]; if(i < arrayLen(arr) ) r&=","; } r &= "]"; return r;
} </cfscript>
<cfset a = [1,2,9,20]> <cfoutput>#inspect(a)#</cfoutput>
Your code should handle arrays, structs, and simple values. For extra credit you can handle queries to by using a bunch of query set sells.
Also note that my test UDF returns a literal value like Groovy. You can also return a series of statements instead:
ob = arrayNew(1); ob[1] = 1; ob[2] = 2; etc
Note that I used "ob" to represent the top level data. Since I pass the variable, and not the variable name, I chose an arbitrary variable name to store the data.
Enjoy!