Skip to main content
July 9, 2008
Question

Array of structures - two ways to approach - which is better?

  • July 9, 2008
  • 4 replies
  • 348 views
Folks - I've been trying to create an array of structures, but I bumped into problem that lead me to an example that is completely different. I'm trying to figure out which is valid. In general, it comes down to this:

(1) myStructure .firstName
vs
(2) myStructure.firstName


The problem with the first is that I can't find a way to discover the size of the array, which leads me to suspect this is not a valid method. The problem with the second is that you must redefine the structure for EACH array element, which seems like it would add a lot of overhead.

Any advice? Sample code attached.

Thanks!

************* Sample (1) **************
<CFSET sCust=StructNew()>
<CFSET vCustCount = #rsMailAuth.recordcount#>
<CFSET i = 0>

<CFOUTPUT QUERY="rsMailAuth">
<CFSET i = i+1>
<CFSET sCust.userId = #rsMailAuth.userId#>
<CFSET sCust.validEmailSource
= #rsMailAuth.validTcEmails#>
<CFSET sCust.authKeyword = #rsMailAuth.tcEmailKeyword#>
<CFSET sCust.authKeywordLoc
= #rsMailAuth.tcEmailKeywordLoc#>
</CFOUTPUT>


************* Sample (2) **************
<cfset strTest = arrayNew(1) >
<cfset strTest[1] = structNew() >
<CFLOOP INDEX = "i" FROM="1" TO="5" >
<cfset strTest = structNew() >
<cfset strTest
.id = i>
<cfset strTest .name = "#i#name@mail.com">
</CFLOOP>

<cfoutput>#arrayLen(strTest)#</cfoutput>
    This topic has been closed for replies.

    4 replies

    July 9, 2008
    If you are starting with data from a query, there is a UDF at CFLib that will do the work for you.

    See here:

    http://www.cflib.org/udf/QueryToArrayOfStructures
    Inspiring
    July 9, 2008
    You allude to a query named rsMailAuth, which, based on your Sample (1) already has the data you are trying to build your structures with. If that's true, what are you planning to do with these structures that you can't do with the query.

    Regarding what is valid in the world of cf8, I wouldn't worry about it. As long as your code does what it's supposed to reasonably efficiently, it's valid.
    July 9, 2008
    Sorry - simple english...

    I am collecting a series of emails from a pop email server. As I parse these into the individual parts (subject, body, from, etc) I want to stuff them into a structure. each email would be a new index. so, the 2nd email 'from' info would go into myStructure.from[2] or myStructure[2].from.

    I know there are lots of ways around this, I just have a natural predisposition to use structures when possible as it makes it easier for me to keep track of things and makes it easier to follow the code.

    I can make either of these work, I'm just not sure if they are valid in the world of coldfusion8.
    Inspiring
    July 9, 2008
    Both methods look like serious cases of overengineering? In plain and simple english, without using any programming terminology, what are you trying to accomplish?