Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

referencing a structure within an array

Status
Not open for further replies.

BlastRadius

Programmer
Nov 13, 2001
38
0
0
CA
I have created an array and filled it's values with structures. Now how do I get the info back out. I'll include the code.

<cfset arraySpread = arraynew(1)>
<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;#rsBudgetTrans.recordCount#&quot;>
<!---Create a structure to hold each row of the speadsheat--->
<cfset structTemp = structNew()>
<cfloop query=&quot;rsBudgetTrans&quot;>
<cfloop query=&quot;rsChqBudget&quot;>
<cfif rsBudgetTrans.BudgetID eq rsChqBudget.BudgetID>
<cfset strValue = rsBudgetTrans.BudgetTransactionAmount>
<cfelse>
<cfset strValue = 0>
</cfif>
<cfset strBudget = rsChqBudget.BudgetID>
<cfset structTemp.strBudget = strValue>
</cfloop>
<cfloop query=&quot;rsSaveBudget&quot;>
<cfif rsBudgetTrans.BudgetID eq rsSaveBudget.BudgetID>
<cfset strValue = rsBudgetTrans.BudgetTransactionAmount>
<cfelse>
<cfset strValue = 0>
</cfif>
<cfset strBudget = rsSaveBudget.BudgetID>
<cfset structTemp.strBudget = strValue>
</cfloop>
<cfset structTemp.Date = rsBudgetTrans.TransactionDate>
<cfset structTemp.Description = rsBudgetTrans.TransactionDescription>
<cfset structTemp.Amount = rsBudgetTrans.TransactionAmount>
</cfloop>
<cfset arraySpread = structTemp>
</cfloop>

now to get it back out I have tries alot of diferent things but I will include my last attemp.

<cfoutput>#arraySpread[1].structTemp[&quot;Date&quot;]#</cfoutput>

Does cold fusion let you store stuctures in arrays?
 
I think there are a couple of things going on.

First, the way structures are initialized, you can just set a variable/container to the value of a structure, you must use the StructCopy() or Duplicate() functions.

Also,
Code:
<cfset arraySpread = structTemp>
isn't quite right... you need to set the element of an array to a value, not the array itself. You would either need to add values like
Code:
<cfset arraySpread[i] = structTemp>
or, better yet, use the ArrayAppend() so elements are never filled with null values.

In addition, the structure becomes the array element... so you don't reference it by the structure name.

For example... this works... interpret and manipulate as needed:

Code:
<cfset arraySpread = arraynew(1)>
<!--- Create a structure to hold each row of the speadsheat --->

<cfset structTemp = structNew()>
<cfset structTemp.strBudget = &quot;hello&quot;>
<cfset structTemp.strMoney = &quot;world&quot;>
<cfset structTemp.strDate = &quot;2002/11/07&quot;>

<cfset arrayaddsuccess = ArrayAppend(arraySpread, Duplicate(structTemp))>

<!--- housecleaning... ALWAYS delete the temporary structure --->
<cfset clearsuccess = StructClear(structTemp)>
	

<!--- now you can access the structure values like this --->
<cfoutput>#arraySpread[1][&quot;strDate&quot;]#</cfoutput>

And if you plan on using ArrayToList... you'll want to throw an ArraySet() in there too (I found that out the hard way).

Hope it helps,
-Carl
 
Sorry... that first &quot;First&quot; should read:

&quot;First, the way structures are initialized, you can't just set a variable/container to the value of a structure, you must use the StructCopy() or Duplicate() functions.&quot;

been doing that a lot lately. [neutral] Hope it helps,
-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top