Orange is my favorite color

When I’m writing Javascript or Actionscript, I like using JSON-style notation for creating objects and variables:

foo = {"status": true
,"name": "Brian"
,"values": [0, 5, 11, 93, 2] }

In ColdFusion, the equivalent would be the tedious:

foo = structNew();
foo.status = true;
foo.name = "Brian";
foo.values = arrayNew(1);
arrayAppend(foo.values, "0");
arrayAppend(foo.values, "5");
arrayAppend(foo.values, "11");
arrayAppend(foo.values, "93");
arrayAppend(foo.values, "2");

The JSON notation is much cleaner. It would be nice if CFSCRIPT supported this in a future release. While thinking about an easier way to create a structure from an arbitrary number of arguments, I came up with the following solution:

<cffunction name="createStruct">
<cfreturn arguments>
</cffunction>

<cfset myStruct = createStruct(status = true
,name = "Brian")>

That solves the struct issue but not the array issue. While I’m thinking about it, if you don’t pass in named parameters, isn’t the arguments just an array? I haven’t tested it, but I think we could do this:

<cffunction name="createObj">
<cfreturn arguments>
</cffunction>

<cfset myStruct = createObj(status = true
,name = "Brian"
,values = createObj(0, 5, 11, 93, 2))>

Not really as readable as JSON unfortunately, but does cut down on a lot of lines of code when you are manipulating things by hand.

3 Comments

  1. Dan G. Switzer, II said:

    on August 18, 2006 at 1:06 pm

    To create an array, just write a UDF called arrayCreate() (I use structCreate() and arrayCreate() to be more consistent w/CF nomenclature) you need to loop through the arguments array. It’s not a true array–but some kind of hybrid struct/array.

    If you don’t create the array by looping through the arguments array, some of the array functions won’t work on it.

    I actually use both UDFs in my work as it does make it much easier to create arrays and structures.

    I’d really like to see these functions become native to CF.

    PS – The reason I don’t like using the listToArray() is you can’t use the delimiter in your values or it creates a problem. Having a true UDF that converts an list of arguments to an array is much more useful.

  2. brian said:

    on August 18, 2006 at 1:59 pm

    Good points. Nathan Dintenfass also mentioned that you could reduce the amount of typing using the foo.values[1] = 0; foo.values[2] = 5; syntax, but I would prefer a function based shortcut (if we can’t have “CFON”).

    Where do you store these for easy access? App scope? Request scope? CFC?

  3. Gatzby said:

    on September 19, 2006 at 6:05 am

    A quick way to create an array is to use the ListToArray() function:

    myArray=ListToArray(“alpha,beta,gamma,delta”);

    No such trick for structs, though. I used a custom tag for a while, but that isn’t much use in cfscript:

    <cf_struct _name="greek" first="alpha" seond="beta" third="gamma" [...] />

    The custom tag simply loops through it’s attributes struct and uses the _name attribute to create a struct of that name in the caller scope.

{ RSS feed for comments on this post}