1
votes

I am trying to convert the results of a query into an array

this.arLibrary  = ValueList(qryLibrary.ID).ListToArray();

And I get the following error

Detail A script statement must end with ";".

The CFML compiler was processing:

  • A script statement beginning with this.arLibrary on line 43, column 9.
  • A cfscript tag beginning on line 21, column 2.
KnownColumn -1 KnownLine -1
KnownText <unknown> Line 43 Message Invalid construct.
Snippet this.arLibrary = ValueList(qryLibrary.ID). StackTrace

This does work

temp    = ValueList(qryLibrary.ID);
this.arMetricLibActive  = temp.ListToArray();

It makes me wonder if ValueList() is a string

3
File this to bugbase.adobe.com under Language? However, I have a feeling it'll fall under "Won't Fix". - Henry
Given that array functions work on query columns, you might be looking to do something unnecessary. - Dan Bracuk
Is a ValueList() a string? writeDump(temp.getClass().name); - Leigh
@dan baruk, not in Lucee they don't. That is an undocumented behavior only present in Adobe CF. Lucee has a dedicated function to retrieve a query's column data as an array. - Brad Wood
@Leigh: It returns: java.lang.String. On a similar note ValueList(qryLibrary.ID).getClass(); crashes - James A Mohler

3 Answers

3
votes

Yes, it's a string. The error is a parsing issue in the CFML engine. The same syntax works fine in Lucee. File a bug like Henry suggested.

Here's an example in the CommandBox REPL

CFSCRIPT-REPL: foo = queryNew('bar')
{
    "COLUMNS":[
        "BAR"
    ],
    "DATA":[

    ]
}
CFSCRIPT-REPL: valueList( foo.bar ).listToArray()
[

]
2
votes

James, it'd be useful if you read error messages when they are presented to you: they generally contain pertinent information. I don't mean this in a "stating the obvious" sort of way, but rather that it's actually a very important part of troubleshooting problems. You are faced with an error message from the compiler, which means the error occurred when the source code was being compiled. However you are asking a question about data types, which - in a loosely and dynamically typed language like CFML - is a runtime consideration. "Runtime" implies "when the code is being run" which is intrinsically after the code is compiled. If the code can't compile: it won't be run.

So the issue is not whether valueList() returns a string or anything like that.

The issue here is that there is a bug in ColdFusion's CFML parser, and it is not able to interpret this expression:

ValueList(qryLibrary.ID).ListToArray()

I don't know why there's a problem with this: there should be no problem with parsing the calling of a method on another function call's return value; and indeed it seems to be a peculiarity of using valueList() like this, as opposed to built-in functions in general.

File a bug.

As for what to do about it in your code in the meantime, I think Dan is right: generally one can use a query column as an array anyhow, provided one uses bracket notation to reference the column, eg: qryLibrary["ID"]. Brad draws attention to this not working on Lucee, but... this is neither here nor there, and just something that Lucee needs to deal with. There was a bug raised for this in Railo - https://issues.jboss.org/browse/RAILO-641 - but they declined to address it, with only semi-valid reasoning.

1
votes

Epilog:

This works on ColdFusion 2016 and above

<cfscript>
qryLibrary = QueryNew("ID", "varchar");

qryLibrary.addrow({"id" : "cat"});
qryLibrary.addrow({"id" : "dog"});
qryLibrary.addrow({"id" : "fish"});

writedump(qryLibrary);

arLibrary  = ValueList(qryLibrary.ID).ListToArray();

writedump(arLibrary);
</cfscript>

https://cffiddle.org/app/file?filepath=6588296c-5e4d-49a4-894b-4986513e9e30/0ecde857-6d28-4e43-88a7-7830c109ab11/84cd7e81-16f8-43d7-b4c9-5490b1b5d007.cfm