ColdFusion 8 inline array syntax doesn’t work everywhere
I was trying to use QueryAddColumn() today to add an additional column to a single-row array I’m working with. The 4th parameter to QueryAddColumn is an array of values to add to the query - 1 element in the array for each row in the query. Since my query only has on row in it, I thought this would be a good place to use ColdFusion 8’s new inline array syntax.
<cfset QueryAddColumn(qryFoo,
"newColName",
"varchar",
["my additional value"])>
But this throws an unhelpful “Missing argument name” error:
Missing argument name. When using named parameters to a function, every parameter must have a name.
It took me a while to figure out it just didn’t like using the new array syntax inside the function. Doing it this way worked fine:
<cfset myArray = ["my additional value"]>
<cfset QueryAddColumn(qryFoo,"newColName","varchar",myArray)>
But having to do it that way kind of defeats the point, it doesn’t use any less lines than the old CF7 syntax:
<cfset myArray[0] = "my additional value">
<cfset QueryAddColumn(qryFoo,"newColName","varchar",myArray)>
And it means I have to “var” the myArray variable at the top of my function now. Hopefully Adobe will get this straightened out at some point.
December 21st, 2007 at 7:06 pm
I don’t think there’s anything that needs straightening out. The correct way of doing it makes perfect sense, while your shortcut doesn’t. You’re trying to toss a value into an array within the QueryAddColumn function … but you aren’t giving that array a name. How could CF possibly know into which array you want to value added? How could you refer to that value later? I can’t think of any programming language that would let you add a value to an unnamed array, or any unnamed variable for that matter.
December 21st, 2007 at 10:07 pm
I put in a bug report / feature request a few weeks ago about this. There are so many places where this could be used but just doesn’t function. There is also no nesting of the array/struct syntax to create an array of structs, which I often build up for configuration or test values.
This would be awesome but doesn’t work:
Instead you have to either expressly set the array indexes:
*repeated for 2 and 3
or use a temporary variable:
* repeated two more times
December 21st, 2007 at 10:19 pm
Here the code from my post with the greater than and less thans escaped:
===
This would be awesome but doesn’t work:
<cfset test = [{name=”Dan”,age=30,state=”VA”},
{name=”Chris”,age=23,state=”DC”},
{name=”Todd”,age=53,state=”MA”}]>
Instead you have to either expressly set the array indexes:
<cfset test = arrayNew(1)>
<cfset test[1] = {name=”Dan”,age=30,state=”VA”}>
*repeated for 2 and 3
or use a temporary variable:
<cfset test = arrayNew(1)>
<cfset tmp = {name=”Dan”,age=30,state=”VA”}>
<cfset arrayAppend(test,tmp)>
* repeated two more times
===
Rob,
The array is an argument for another function which does not perform an action on that array. It uses the array as a source of values to add a new column to a query variable.
I think Adobe will make this available in the future.
December 22nd, 2007 at 10:30 am
by the way, you would also expect to be able use arraylength + 1 for the index but that doesn’t work either though it work fine for other values and variables
<cfset test = arrayNew(1)>
<cfset test[arrayLen(test)+1] = {name=”Dan”,age=27,state=”VA”}>
*boom…. error*
January 16th, 2008 at 5:08 pm
The 4th attribute of QueryAddRow is a rowcolumn, why would you expect an array to work there?
Also, why would you use:
When you can ArrayAppend()?
Also, your test above isn’t accurate. Array index in CF -starts- at zero. Your code is telling it to bypass zero and set 1…?
January 16th, 2008 at 5:18 pm
Blargh. Java, get out of my head for 2 minutes. Sorry, CF index begins at 1. But, anyway…
<cfset test = arrayNew(1)>
<cfset test[1] = “foo”>
<cfset test[arrayLen(test)] = {name=”Dan”,age=27,state=”VA”}>
<cfdump var=”#test#”>
That works. So, the issue is the evaluation of the arrayLen() +1 in there.
February 24th, 2008 at 11:21 am
@Rob: I believe Java, JavaScript, C++, and PHP all allow anonymous arrays. The array value is simply assigned to the variable name given to its corresponding argument in the function/method source.
March 9th, 2008 at 1:09 pm
thanks for codes
April 12th, 2008 at 7:29 am
Here is a link that explores CF 8’s inline array syntax a bit more with regards to structs and arrays.
http://www.bennadel.com/blog/740-Learning-ColdFusion-8-Implicit-Struct-And-Array-Creation.htm
For me personally, I’m using it as follows for loading static data into arrays.
<script>
aSet=arraynew(2);
i=0;
i++; aSet[i]=[ “Illinois”, “IL”, 21, “Mississippi River; 279 feet, 33rd” ];
i++; aSet[i]=[ “California”, “CA”, 31, “DeathValley; 282 feet below sea level, 1st” ];
// and so on….
</script>
There are some glaring deficiencies with this feature, but I’m glad they finally put it in and hopeful they improve on it in the future.