ColdFusion Structures and Associative Array Notation

I recently had to create a bar chart in ColdFusion using a structure data type. To make it easy to display each bar's label I used the associative array notation for the key names when I created my structure. The associative array notation enables your structure keys to "contains spaces, numbers, or special characters." (Reference 1). I've forgotten in the past that this alternative way of specifying structure keys is available in ColdFusion so this blog entry will remind me (and maybe others) about the associative array notation for structures.

References:

  1. About Structures, CFMX 7 Developer's Guide
  2. Structure Functions
  3. ArrayToList Function

Below is a code example of a structure that uses associative array notation. I left out all the database work and query processing and in this example just hard coded the structure.


<cfset membersByAgeGroup = structNew() >
<cfset membersByAgeGroup["60 years plus"] = 450>
<cfset membersByAgeGroup["40 - 59 years"] = 1200>
<cfset membersByAgeGroup["39 years and younger"] = 800>

Note how the key values are specified inside quotation marks and inside [ ]. Also note that there is no period between the structure name and the key (NOT membersByAgeGroup.["60 years plus"] which I've done before by mistake).

Now I can use the membersByAgeGroup structure to create my chart and have my bar labels be the key values. In this case the key values are "60 years plus", "40 - 59 years", and "39 years and younger". Click here to see the chart.


<cfchart chartheight="400" chartwidth="600" yaxistitle="Number of Members" title="Age Breakdown of Members" fontsize="10" show3d="yes" gridlines="5" showmarkers="yes" showxgridlines="yes" >
<cfchartseries type="horizontalbar" seriescolor="##FF0000" >
<cfloop list="#ArrayToList( StructSort(membersByAgeGroup, "numeric", "desc") )#" index="key" >

     <cfchartdata item="#key#" value="#membersByAgeGroup[key]#" >

</cfloop>
</cfchartseries>
</cfchart>

See the related blog entry below for more information about using a structure to create a chart. Reference 2 above provides information about structure functions such as StructSort and reference 3 provides information about the ArrayToList function.

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Bruce,

Take a look at your loop. Try looping with the collection attribute. That allows you to loop over a structure.

cfloop collection=membersByAgeGroup item=key

Kevin
# Posted By Kevin Schmidt | 5/23/07 6:49 AM
Kevin - thanks for the tip. I don't think I can use the collection attribute for CFloop since I first need to sort my structure. The StructSort function I'm using returns an array.

According to Ray Camden (see: http://ray.camdenfamily.com/index.cfm/2007/5/11/Lo...)

CFMX 8 will include an array attribute for CFLoop, so I won't need to convert the Array to a list.
# Posted By Bruce | 5/23/07 7:24 AM
Your blog came up first on Google.

I like to do things like this too. Post little things that remind me of stuff, and maybe other people will find it helpful.

I was just trying to remember if structs could be associative, and... yup!

Thanks!
# Posted By Brian | 7/24/08 6:27 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner