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.
Rob Huddleston says:
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.
21 December 2007, 7:06 pmDan Roberts says:
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
21 December 2007, 10:07 pmDan Roberts says:
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.
21 December 2007, 10:19 pmDan Roberts says:
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*
22 December 2007, 10:30 amTodd Rafferty says:
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…?
16 January 2008, 5:08 pmTodd Rafferty says:
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.
16 January 2008, 5:18 pmChris says:
@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.
24 February 2008, 11:21 amçeviri says:
thanks for codes
9 March 2008, 1:09 pmTom says:
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.
12 April 2008, 7:29 amRyan Stille says:
I have been told that this issue should be fixed in CF9, or possibly even sooner in an updater for CF8. Whooo!
29 May 2008, 9:43 amSteve Fentriss says:
Aha! Here's a surefire solution to the problem of inline array syntax in Coldfusion 8.
SUMMARY:
Use the listToArray(list [, delimiters ]) function.
SHORT EXAMPLE:
<cfset queryAddColumn(myQuery,"fav_pokemon",listToArray("Pikachu,Bulbasaur,Venomoth,Dewgong,Snorlax"))>
— LONG EXAMPLE: —
<!— create a query which has a "title" column,
but lacks a "notes" column: —>
<cfset Q = queryNew('title')>
<!— stock the query with two rows,
regarding apples and oranges: —>
<cfset queryAddRow(Q)><!— first row —>
<cfset querySetCell(Q,"title","Apples")>
<cfset queryAddRow(Q)><!— second row —>
<cfset querySetCell(Q,"title","Oranges")>
<!— NOW, ADD A NEW COLUMN called "notes",
FROM AN INLINE ARRAY containing values —>
<cfset queryAddColumn(Q,"notes",listToArray("Apples are great , Unless compared to oranges"))>
<!— see if it worked: —>
<cfdump var="#Q#">
— :LONG EXAMPLE —
Hope that helps!
11 October 2009, 4:21 am– Steve