<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Simon Whatley &#187; Germany</title>
	<atom:link href="http://www.simonwhatley.co.uk/tag/germany/feed" rel="self" type="application/rss+xml" />
	<link>http://www.simonwhatley.co.uk</link>
	<description>The opposite of every great idea is another great idea</description>
	<lastBuildDate>Wed, 02 Nov 2011 09:28:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Implicit Structures in ColdFusion 8</title>
		<link>http://www.simonwhatley.co.uk/implicit-structures-in-coldfusion-8</link>
		<comments>http://www.simonwhatley.co.uk/implicit-structures-in-coldfusion-8#comments</comments>
		<pubDate>Fri, 07 Mar 2008 08:23:22 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[associative array]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Dupont]]></category>
		<category><![CDATA[France]]></category>
		<category><![CDATA[Germany]]></category>
		<category><![CDATA[implicit]]></category>
		<category><![CDATA[John Doe]]></category>
		<category><![CDATA[key]]></category>
		<category><![CDATA[key-value]]></category>
		<category><![CDATA[London]]></category>
		<category><![CDATA[One]]></category>
		<category><![CDATA[pairs]]></category>
		<category><![CDATA[Simon Whatley]]></category>
		<category><![CDATA[Spain]]></category>
		<category><![CDATA[struct]]></category>
		<category><![CDATA[structNew]]></category>
		<category><![CDATA[structures]]></category>
		<category><![CDATA[Three]]></category>
		<category><![CDATA[Two]]></category>
		<category><![CDATA[value]]></category>
		<category><![CDATA[version 8]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=435</guid>
		<description><![CDATA[In an earlier post I eluded to the <a href="/implicit-arrays-in-coldfusion-8" title="Implicit Arrays in ColdFusion 8">implicit creation of arrays</a> in ColdFusion 8. Well, the same can be said of structures.

A structure, also known as an associative array, is a complex data type composed of a collection of keys and a collection of values, where each key is associated with one value (a key-value pair). The operation of finding the value associated with a key is called a lookup or indexing, and this is the most important operation supported by a structure. The relationship between a key and its value is sometimes called a mapping or binding. For example, if the value associated with the key "Age" is 29 and "City" is "London", we say that our structure maps "Age" to 29 and "City" to "London".]]></description>
			<content:encoded><![CDATA[<p>In an earlier post I eluded to the <a href="/implicit-arrays-in-coldfusion-8" title="Implicit Arrays in ColdFusion 8">implicit creation of arrays</a> in ColdFusion 8. Well, the same can be said of structures.</p>
<p>A structure, also known as an associative array, is a complex data type composed of a collection of keys and a collection of values, where each key is associated with one value (a key-value pair). The operation of finding the value associated with a key is called a lookup or indexing, and this is the most important operation supported by a structure. The relationship between a key and its value is sometimes called a mapping or binding. For example, if the value associated with the key &#8220;Age&#8221; is 29 and &#8220;City&#8221; is &#8220;London&#8221;, we say that our structure maps &#8220;Age&#8221; to 29 and &#8220;City&#8221; to &#8220;London&#8221;.</p>
<p>Using structures, you can call the array element you need using a string rather than a number, which is often easier to remember. The downside is that these aren&#8217;t as useful in a loop because they do not use numbers as the index value.</p>
<p>We can think of an address book as a good example of a structure. The classic way of creating and assigning key-values pairs to a structure, in earlier versions of ColdFusion, would be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;">strPerson <span style="color: #0000ff">=</span> <span style="color: #800080;">structNew</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</span>
<span style="color: #000099;">strPerson.firstName <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Jean&quot;</span>;</span>
<span style="color: #000099;">strPerson.lastName <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Dupont&quot;</span>;</span>
<span style="color: #000099;">strPerson.city <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Paris&quot;</span>;</span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p>Or an alternative method uses array-notation to create the necessary key-value pairs:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;">strPerson <span style="color: #0000ff">=</span> <span style="color: #800080;">structNew</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</span>
<span style="color: #000099;">strPerson<span style="color: #000000;">&#91;</span><span style="color: #009900;">&quot;Firstname&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Hans&quot;</span>;</span>
<span style="color: #000099;">strPerson<span style="color: #000000;">&#91;</span><span style="color: #009900;">&quot;Lastname&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Mustermann&quot;</span>;</span>
<span style="color: #000099;">strPerson<span style="color: #000000;">&#91;</span><span style="color: #009900;">&quot;Country&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Germany&quot;</span>;</span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p>NB. When using the array-notation, the key names keep their case. However, running the following code results in the value &#8220;France&#8221; being overwritten with &#8220;Germany&#8221;, even though the key name is a different case. This serves to highlight that ColdFusion is not case-sensitive.</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;">strPerson <span style="color: #0000ff">=</span> <span style="color: #800080;">structNew</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</span>
<span style="color: #000099;">strPerson<span style="color: #000000;">&#91;</span><span style="color: #009900;">&quot;Country&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;France&quot;</span>;</span>
<span style="color: #000099;">strPerson<span style="color: #000000;">&#91;</span><span style="color: #009900;">&quot;COUNTRY&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Germany&quot;</span>;</span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p><strong>Implicit Structures</strong></p>
<p>With the introduction of implicit structures in ColdFusion 8, the creation of structures is greatly simplified. For example, rather than having to use the <code>structNew()</code> function, we can now simply do the following:</p>
<p>Using strings for values:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;">myStruct <span style="color: #0000ff">=</span> <span style="color: #000000;">&#123;</span>firstname<span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;Simon&quot;</span>, lastname<span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;Whatley&quot;</span>, city<span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;London&quot;</span><span style="color: #000000;">&#125;</span>;</span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p><img src='http://www.simonwhatley.co.uk/blog/wp-content/uploads/2008/02/implicit-structure-string-key-value.png' alt='Implicit Structures - Strings as Keys and Values' /></p>
<p>Using integers for values:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;">myStruct <span style="color: #0000ff">=</span> <span style="color: #000000;">&#123;</span>account_no<span style="color: #0000ff;">=</span><span style="color: #ff0000;">12345678</span>, s<span style="color: #0000ff;">or</span>t_code<span style="color: #0000ff;">=</span><span style="color: #ff0000;">123456</span><span style="color: #000000;">&#125;</span>;</span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p><img src='http://www.simonwhatley.co.uk/blog/wp-content/uploads/2008/02/implicit-structure-integer-value.png' alt='Implicit Structures - Integers as Values' /></p>
<p>Using integers as keys:</p>
<p>This example most closely represents an array since arrays have numeric keys.</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;">myStruct <span style="color: #0000ff">=</span> <span style="color: #000000;">&#123;</span><span style="color: #ff0000;">10001</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;John&quot;</span>, <span style="color: #ff0000;">10002</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;Doe&quot;</span>, <span style="color: #ff0000;">10003</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;New York&quot;</span><span style="color: #000000;">&#125;</span>;</span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p><img src='http://www.simonwhatley.co.uk/blog/wp-content/uploads/2008/02/implicit-structure-integer-key.png' alt='Implicit Structures - Integers as Keys' /></p>
<p>The integer could represent the unique identifier of an object, for example, user ID or order ID. Therefore, if we had nested structures like below, 10001 would be the ID of Simon Whatley, whilst 10002 would be the ID of John Doe.</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;">myStruct1 <span style="color: #0000ff">=</span> <span style="color: #000000;">&#123;</span>firstname<span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;Simon&quot;</span>, lastname<span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;Whatley&quot;</span>, city<span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;London&quot;</span><span style="color: #000000;">&#125;</span>;</span>
<span style="color: #000099;">myStruct2 <span style="color: #0000ff">=</span> <span style="color: #000000;">&#123;</span>firstname<span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;John&quot;</span>, lastname<span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;Doe&quot;</span>, city<span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;New York&quot;</span><span style="color: #000000;">&#125;</span>;</span>
<span style="color: #000099;">myStruct3 <span style="color: #0000ff">=</span> <span style="color: #000000;">&#123;</span><span style="color: #ff0000;">10001</span><span style="color: #0000ff;">=</span>myStruct1, <span style="color: #ff0000;">10002</span><span style="color: #0000ff;">=</span>myStruct2<span style="color: #000000;">&#125;</span>;</span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p><img src='http://www.simonwhatley.co.uk/blog/wp-content/uploads/2008/02/implicit-structure-nested-struct.png' alt='Implicit Structures - Nested Structures' /></p>
<p>Mixed data types:</p>
<p>It is possible to mix the data types in an structure. For example, we can use an Integer, String and Array as elements within an array, with no problems. Since we need to know the key name before accessing the value, it is also likely we will know the type of the value and will be able to handle it accordingly. However, never assume this is always the case, so type checking is necessary when retrieving the data.</p>
<p>The example below demonstrates the ability to add arrays to structures.</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;">myArray1 <span style="color: #0000ff">=</span> <span style="color: #000000;">&#91;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">3</span><span style="color: #000000;">&#93;</span>;</span>
<span style="color: #000099;">myArray2 <span style="color: #0000ff">=</span> <span style="color: #000000;">&#91;</span><span style="color: #009900;">&quot;One&quot;</span>,<span style="color: #009900;">&quot;Two&quot;</span>,<span style="color: #009900;">&quot;Three&quot;</span><span style="color: #000000;">&#93;</span>;</span>
<span style="color: #000099;">myStruct <span style="color: #0000ff">=</span> <span style="color: #000000;">&#123;</span>array1<span style="color: #0000ff;">=</span>myArray1, array2<span style="color: #0000ff;">=</span>myArray2<span style="color: #000000;">&#125;</span>;</span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p><img src='http://www.simonwhatley.co.uk/blog/wp-content/uploads/2008/02/implicit-structure-array-values.png' alt='Implicit Structures - Nested Arrays' /></p>
<p>Structures, by their nature, cannot be sorted by value, only by the key name. They are best for related data, where order is not important and direct access to an individual element is important. Many of ColdFusion&#8217;s variable scopes can be accessed as structures, for example, Server, Application, Session and Variables etc.</p>
<p><strong>Words of Caution</strong></p>
<p>Implicit structures do have their limitations. For example, you cannot nest implicit struct, or indeed array, creation.</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;">myStruct1 <span style="color: #0000ff">=</span> <span style="color: #000000;">&#123;</span></span>
<span style="color: #000099;">	myStruct2 <span style="color: #0000ff">=</span> <span style="color: #000000;">&#123;</span></span>
<span style="color: #000099;">		firstName <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Jean&quot;</span>,</span>
<span style="color: #000099;">		lastName <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Dupont&quot;</span>,</span>
<span style="color: #000099;">		country <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;France&quot;</span></span>
<span style="color: #000099;">	<span style="color: #000000;">&#125;</span>,</span>
<span style="color: #000099;">	myStruct3 <span style="color: #0000ff">=</span> <span style="color: #000000;">&#123;</span></span>
<span style="color: #000099;">		firstName <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Juan&quot;</span>,</span>
<span style="color: #000099;">		lastName <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Pablo&quot;</span>,</span>
<span style="color: #000099;">		country <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Spain&quot;</span></span>
<span style="color: #000099;">	<span style="color: #000000;">&#125;</span></span>
<span style="color: #000099;"><span style="color: #000000;">&#125;</span></span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p>The above will throw the following parsing error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">coldfusion.compiler.ParseException:
Invalid CFML construct found on line 3 at column 10.</pre></div></div>

<p><strong>UPDATE:</strong> The recent <a href="http://www.adobe.com/support/coldfusion/downloads_updates.html#cf8" title="ColdFusion Update" target="_blank" rel="nofollow">ColdFusion Update</a> now includes the ability to nest implicit structures.</p>
<p>To get around this problem, you can create each structure individually and then use the structure as the value in a key-value pair (as seen in the nested structure example above).</p>
<p>A (possible) strength of ColdFusion is that you can add key-value pairs as many times as is necessary. This is the same for explicit and implicit structure creation. However, the following code and screenshot serves to demonstrate that whether you explicitly or implicitly create a structure, if you duplicate a key, the last key-value pair in the sequence is the one that is represented in the structure:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;">myStruct <span style="color: #0000ff">=</span> <span style="color: #000000;">&#123;</span></span>
<span style="color: #000099;">	firstName <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Jean&quot;</span>,</span>
<span style="color: #000099;">	lastName <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Dupont&quot;</span>,</span>
<span style="color: #000099;">	country <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;France&quot;</span>,</span>
<span style="color: #000099;">	country <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Germany&quot;</span></span>
<span style="color: #000099;"><span style="color: #000000;">&#125;</span>;</span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p><img src='http://www.simonwhatley.co.uk/blog/wp-content/uploads/2008/03/implicit-structure-duplicate-key.png' alt='Implicit Structures - Duplicate Keys' /></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/implicit-structures-in-coldfusion-8/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

