I need to fetch a value from an Array of Structures. In the object below, I want to fetch the value column of the struct, which is nested inside the array of struct. I think I can use structFindValue, but am not sure.
Here is the screenshot for this:
What I have tried is this:
<cfdump var=#StructFindValue(emails,"value","all")# />
Please guide me how can I find the value.
ok i found it like this, not a good way but working, as of now, i ned only 1 array:
<cfset result = getEmailAddr.emails[1].value>
Related
I am having difficulties trying to use ListAppend.
I have data from a table and this is my code.
<cfquery name="getData" datasource="test">
select * from test;
</cfquery>
Now what I want to do is to make all the values in the column name which I have named it as nm_column into a list using ListAppend.
<cfset dataList = ListAppend('', '#getData.nm_column#')>
<cfoutput>#dataList#</cfoutput>
What this does is that it only shows the 1st value of the nm_colum. I do understand that I am missing the loop part, thats why its only showing me just the 1st value. So how do I loop it and get all the values to it?
I tried this, but it did not work.
<cfset dataList = ListAppend('', '<cfloop query="getData">#getData.nm_column#</cfloop>')>
So can someone please teach me the way to properly write that code?
There's a built in function in ColdFusion that will do this for you.
<cfset dataList = valueList(getData.nm_column)>
As far as the issue with your code, listAppend's first argument is the list you're adding things to. Also, you cannot nest ColdFusion tags like that. The code will not compile.
If you want to loop through something to append to a list, this is what you would do.
<cfset dataList = ''>
<cfloop query="getData">
<cfset dataList = listAppend(dataList, nm_column)>
</cfloop>
This would be terrible for performance though since a string is immutable. If you really needed to add items to a list through a lip I would create an array and then use arrayToList to convert that array to a list.
I have this ColdFusion struct that I dumped:
I'm trying to drill into this farther to get the DTCREATED variable.
This was generated from the following query:
gotResults = queryService.execute(sql="SELECT * FROM someTBL WHERE EXPORTID in
(#whereVariable#)" );
Can anyone tell me how to drill further into this ColdFusion struct?
Use gotResults.getResult().DTCREATED.
gotResults.getResult() returns a Query object, so just use .DTCREATED (case-insensitive) to get to the column. It will return the value in the first row of that column by default.
I've discovered that the valuelist() function doesn't like dynamically named queries:
<cfscript>
variables.nNumber = 1;
request.qDirectories = new query();
request.qDirectories.setDBType('query');
request.qDirectories.setAttributes(qDirectories=request.qAllDirectories);
request.qDirectories.setSQL("SELECT id, name, abbr, multiproperties, isPublished,
isArchived, dateAdded, lastModified, lastModifiedBy,
prefix, lstJournalCodes FROM qDirectories");
request["qDirectories#variables.nNumber#"] = request.qDirectories.execute().getResult();
writeDump(valueList(request["qDirectories#variables.nNumber#"].id));
</cfscript>
Upon discovering this, I thought arrayToList() would help. It does help but it only brings back an array with one value in even if there are multiple rows.
Is there a way to get all the values from a particular column in a dynamically named query?
Copying/referencing the dynamic query to a simpler variable name doesn't help? As in:
tempQry= request["qDirectories#variables.nNumber#"];
valueList(tempQry.id);
I have a column name and, for the sake of SpreadsheetSetCellFormula, I want to get the index of that column from its name. This is important for expandability, as columns may be added or taken away in the future.
When I use queryName.ColumnList, ColdFusion automatically alphabetizes the list. However, passing it into SpreadsheetAddRows dumps the columns in original order. How can I get the index of a column from its name?
<!--- get the column list, in the original order, as a coldfusion compatible array --->
<cfset variables.columnArray = createObject("java","java.util.Vector").init(
createObject("java","java.util.Arrays").asList(
query.getColumnList()
)
)/>
<!--- get the index of the column. note that this is case sensitive. --->
<cfset variables.myColumnIndex = variables.columnArray.indexOf("MY_COLUMN")/>
No looping required.
This doesn't directly answer your question, however this may be VERY useful in your situation (I have used it in a similar situation)
This is taken from:
http://existdissolve.com/2010/11/quick-coldfusion-goodness/
columns = arrayToList(myquery.getMeta().getcolumnlabels())
gives you a list of the columns in their original order, with their original case sensitivity (not all upper case)
You can use the getMetaData() function, which you can read about here. This function will return an array of Columns in the correct order, including some other information. You would use it as follows:
getMetaData(queryName)
I have a table in a form with a loop around it that sets an index on all the name fields (form variables) that changes based on user input (user dictates how many tables get built).
Once submitted, I take these variables and create a struct out of it. I know that a query object is already a struct, but I have to have each tables names unique, then put them into a struct which I can then rename to insert into the DB.
My problem is how do I write this correctly and efficiently? I need to have a variable with a nested variable that is my index to make the name unique for each iteration through the loop. I have tried many combinations of pound signs and quotes and can't get it.
If there is a better way to do this I am up for that too!
dot notation
cfset myStruct#i#=StructNew()>
cfset myStruct#i#.ID#i#="#form.myVarA#i##"
cfset myStruct#i#.s1#i#="#form.myVarB#i##"
cfset myStruct#i#.s2#i#="#form.myVarC#i##"
associated array notation
cfset myStruct#i#=StructNew()>
cfset myStruct#i#[ID#i#]="#form.myVarA#i##"
cfset myStruct#i#[s1#i#]="#form.myVarB#i##"
cfset myStruct#i#[s2#i#]="#form.myVarC#i##"
Any help is greatly appreciated.
This is the best reference you'll ever need to understand variables in CFML:
http://www.depressedpress.com/Content/Development/ColdFusion/Guides/Variables/Index.cfm
To answer your question, try this:
<cfset myStruct["#i#"] = structNew() />
<cfset myStruct["#i#"]["ID#i#"] = form["myVarA#i#"] />
<cfset myStruct["#i#"]["s1#i#"] = form["myVarB#i#"] />
This should give you:
myStruct.1.id1 = form.myvarA1
myStruct.1.s11 = form.myvarB1