Hi there,
We have a home-grown arrayToStruct function - so that we can customize the key names - as opposed to using the built-in function - which uses the index as the key name.
public array function arrayToStruct(required array arr, required string columnnames='col1') {
var newArr = [];
var newStruct = {};
var rows= ArrayLen(arguments.arr);
var columns = listlen(arguments.columnnames);
var thisArr = [];
if(ArrayLen(arguments.arr) GT 0 AND (ArrayLen(arguments.arr[1]) GTE columns)) {
for(var i=1;i <= rows;i++) {
thisArr = arr[i];
newStruct = {};
for(var j=1; j <= columns; j++) {
try {
newStruct["#listGetAt(arguments.columnnames,j)#"] = thisArr[j];
} catch(any e) {
newStruct["#listGetAt(arguments.columnnames,j)#"] = '';
}
}
arrayAppend(newArr,duplicate(newStruct));
}
}
return newArr;
If I use the following code (utilizing our function);
(Please note:
There are NO SPACES after the “commas” of the list items.
There are ONLY TABS
between the start of the line and the next item in the list.
)
qOrigFieldsXX = application.dataformat.arrayToStruct(
qOrigFields,
"
dsName,
colId,
colName,
colDataTypeId,
colDataTypeDescr,
colDataTypeFormat,
colPosInFile
"
I get a space inserted at the beginning of the keyName of my Struct.
It isn’t instantly obvious, either - because when you dump the structKeyList() : you get the following;
string colDataTypeFormat, colId, colDataTypeId, colName, colPosInFile, dsName, colDataTypeDescr
Which looks appropriate… a comma, then a space, then the value, then a comma, then a space…
Seems like a “normal” list to me!
I can fix the issue - now that I know what it is doing by using the following code;
qOrigFieldsXX = Application.dataformat.arrayToStruct(
qOrigFields,
"dsName,colId,colName,colDataTypeId,colDataTypeDescr,colDataTypeFormat,colPosInFile"
I can’t work out if it is some funkiness with our function that I am not seeing or something else?
But am hoping that someone here might spot the problem?
After the above edit our code is fine and dandy, but I just hate things getting the better of me!
As always : THANKS!
Don’t forget to tell us about your stack!
OS: AWS Linux 2 (CentOS7)
Java Version: 11 (something)
Tomcat Version: 8 (something)
Lucee Version: 5.3.10.133-SNAPHOT