<?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; array</title>
	<atom:link href="http://www.simonwhatley.co.uk/tag/array/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>SQL User-Defined Function: ListToTable</title>
		<link>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable</link>
		<comments>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable#comments</comments>
		<pubDate>Mon, 22 Sep 2008 10:58:30 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[extend]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sub-routine]]></category>
		<category><![CDATA[subroutine]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[tabular]]></category>
		<category><![CDATA[UDF]]></category>
		<category><![CDATA[user defined function]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=426</guid>
		<description><![CDATA[A common need in SQL is the ability to iterate over a list as if it were an array. In SQL it is not possible to declare arrays, unlike other programming languages such as ColdFusion, ActionScript and Java. Fortunately, there is a way around this problem: use a User-Defined Functions (UDFs) to create a tabular version of the data. Arrays are, after all, essentially tabular data (at their simplest, one dimension level).]]></description>
			<content:encoded><![CDATA[<p>A common need in <abbr title="Structured Query Language">SQL</abbr> is the ability to iterate over a list as if it were an <a href="http://en.wikipedia.org/wiki/Array" title="Wikipedia: Array" target="_blank" rel="nofollow">array</a>. In <abbr title="Structured Query Language">SQL</abbr> it is not possible to declare arrays, unlike other programming languages such as ColdFusion, ActionScript and Java. Fortunately, there is a way around this problem: use a <a href="http://en.wikipedia.org/wiki/User-defined_function" title="Wikipedia: User-Defined Functions" target="_blank" rel="nofollow">User-Defined Functions</a> (<abbr title="User Defined Functions">UDF</abbr>s) to create a tabular version of the data. Arrays are, after all, essentially tabular data (at their simplest, one dimension level).</p>
<p>A User-Defined Function, is a function provided by the user of a program or environment. In <abbr title="Structured Query Language">SQL</abbr> databases, a user-defined function provides a mechanism for extending the functionality of the database server by adding a function that can be evaluated in <abbr title="Structured Query Language">SQL</abbr> statements.</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>
<h3>The Function Code</h3>
<p>Below is the complete function definition:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>CREATE</span> <<span style="color: #0000ff;">|</span>/1/>FUNCTION</span> dbo.udf_ListToTable
<span style="color: #66cc66;">&#40;</span>
	@LIST 		<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>,
	@DELIMITER 	<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">=</span> <span style="color: #ff0000;">','</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>RETURNS</span> @ListTable <<span style="color: #0000ff;">|</span>/1/>TABLE</span>
<span style="color: #66cc66;">&#40;</span>
	Item <<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>AS</span>
<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @LenDel 	<<span style="color: #0000ff;">|</span>/2/>INT</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @Pos 		<<span style="color: #0000ff;">|</span>/2/>INT</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @Item 		<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #808080;">--Get the length of the delimiter, use hack to get around LEN(' ') = 0 issue</span>
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @LenDel <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span>@DELIMITER <span style="color: #0000ff;">+</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">-</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @Pos <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">CHARINDEX</span><span style="color: #66cc66;">&#40;</span>@DELIMITER, @LIST<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>WHILE</span> @Pos <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
	<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
		<span style="color: #808080;">--Get the item</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @Item <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@LIST, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, @Pos-<<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Add it to the table (if not empty string)</span>
		<<span style="color: #0000ff;">|</span>/1/>IF</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@Item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
			<<span style="color: #0000ff;">|</span>/1/>INSERT</span> @ListTable <span style="color: #66cc66;">&#40;</span>Item<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@Item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Remove the item from the list</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @LIST <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">STUFF</span><span style="color: #66cc66;">&#40;</span>@LIST, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, @Pos+@LenDel-<<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Get the position of the next delimiter</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @Pos <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">CHARINDEX</span><span style="color: #66cc66;">&#40;</span>@DELIMITER, @LIST<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>END</span>
&nbsp;
	<span style="color: #808080;">--Add the last item to the table (if not empty string)</span>
	<<span style="color: #0000ff;">|</span>/1/>IF</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@LIST<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
		<<span style="color: #0000ff;">|</span>/1/>INSERT</span> @ListTable <span style="color: #66cc66;">&#40;</span>Item<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@LIST<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>RETURN</span>
<<span style="color: #0000ff;">|</span>/1/>END</span>
<<span style="color: #0000ff;">|</span>/1/>GO</span></pre></div></div>

<p>The function simply loops over the list passed into the function. Each list item is then inserted into the variable named <code>@ListTable</code>, which is of type <code>TABLE</code>. The <code>@ListTable</code> variable is then returned out of the function and can be handled the same as any other table.</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>
<h3>The Function In Use</h3>
<p>A simple demonstration is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>INSERT</span> <<span style="color: #0000ff;">|</span>/1/>INTO</span> tableName <span style="color: #66cc66;">&#40;</span>column1, column2, column3, column4<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>SELECT</span> @variable1, @variable2, myTable.item, <span style="color: #ff00ff;">GETDATE</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>FROM</span> dbo.udf_ListToTable<span style="color: #66cc66;">&#40;</span>@list,<span style="color: #ff0000;">','</span><span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>AS</span> myTable</pre></div></div>

<p>In this example, we insert the same information (<code>@variable1</code>, <code>@variable2</code>) for every instance of an item found in <code>myTable</code>.</p>
<p>This is useful, for example, if you want to apply a setting to a group of users. The group of users could be contained in a list that needs to be parsed as a table, whilst the individual setting details are contained in the other variables.</p>
<h3>Download the Code</h3>
<p><a href="/examples/sql/functions/udf_ListToTable.txt" title="Download the code">Download the code</a>, rename the file to .sql and run on your database instance. You will then be able to reference the function in your Stored Procedures.</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/sql-user-defined-function-listtotable/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Java Behind a ColdFusion Array and Structure</title>
		<link>http://www.simonwhatley.co.uk/the-java-behind-a-coldfusion-array-and-structure</link>
		<comments>http://www.simonwhatley.co.uk/the-java-behind-a-coldfusion-array-and-structure#comments</comments>
		<pubDate>Wed, 12 Mar 2008 09:10:48 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Adobe ColdFusion]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[Ben Nadel]]></category>
		<category><![CDATA[BlueDragon]]></category>
		<category><![CDATA[CFML]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[NewAtlanta]]></category>
		<category><![CDATA[Railo]]></category>
		<category><![CDATA[Railo Technologies]]></category>
		<category><![CDATA[struct]]></category>
		<category><![CDATA[structures]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=466</guid>
		<description><![CDATA[Following a conversation with a friend regarding how ColdFusion handles arrays and structures in 'the background', I was interested to find out what Java classes each were mapped to. This was a relatively simple case of using the functions <code>getClass()</code>, <code>getSuperClass()</code> and <code>getName()</code> to parse out the name of the Java classes.]]></description>
			<content:encoded><![CDATA[<p>Following a conversation with a friend regarding how ColdFusion handles arrays and structures in &#8216;the background&#8217;, I was interested to find out what Java classes each were mapped to. This was a relatively simple case of using the functions <code>getClass()</code>, <code>getSuperClass()</code> and <code>getName()</code> to parse out the name of the Java classes.</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #808080; background-color:#ffff80;">&lt;!--- Arrays (repeat up the hierarchy) ---&gt;</span></span>
<span style="color: #333333;"><span style="color: #800000;">&lt;cfdump</span> <span style="color: #0000ff;">var</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;#arrayNew(1).getClass().getName()#&quot;</span><span style="color: #800000;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #800000;">&lt;cfdump</span> <span style="color: #0000ff;">var</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;#arrayNew(1).getClass().getSuperClass().getName()#&quot;</span><span style="color: #800000;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #808080; background-color:#ffff80;">&lt;!--- Structures (repeat up the hierarchy) ---&gt;</span></span>
<span style="color: #333333;"><span style="color: #800000;">&lt;cfdump</span> <span style="color: #0000ff;">var</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;#structNew().getClass().getName()#&quot;</span><span style="color: #800000;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #800000;">&lt;cfdump</span> <span style="color: #0000ff;">var</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;#structNew().getClass().getSuperClass().getName()#&quot;</span><span style="color: #800000;">&gt;</span></span></pre></div></div>

<p>What became immediately apparent was that both were handled differently. In the case of a ColdFusion array, the parent class was the <code>java.util.Vector</code>, which seemed obvious. The <code>Vector</code> class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a <code>Vector</code> can grow or shrink as needed to accommodate adding and removing items after the <code>Vector</code> has been created.</p>
<p>ColdFusion structures, however, are handled entirely differently, with the parent class being a <code>coldfusion.runtime</code> class. In fact, as we can see below, the code to handle a ColdFusion structure is entirely written for ColdFusion until the root class of <code>java.lang.Object</code>. Clearly ColdFusion does some complex wizardry to handle the complex nature of structues (associative arrays).</p>
<p>The class hierarchy for an Adobe ColdFusion array:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.lang.Object
	java.util.AbstractCollection
		java.util.AbstractList
			java.util.Vector
				coldfusion.runtime.Array</pre></div></div>

<p>The class hierarchy for an Adobe ColdFusion structure:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.lang.Object
	coldfusion.util.CaseInsensitiveMap
		coldfusion.util.FastHashtable
			coldfusion.runtime.Struct</pre></div></div>

<p>Being an inquisitive person, I applied the same code to <a href="http://www.railo-technologies.com/en/index.cfm" title="Railo Technologies CFML Server" target="_blank" rel="nofollow">Railo</a> and <a href="http://www.newatlanta.com/products/bluedragon/index.cfm" title="New Atlanta BlueDragon CFML Server" target="_blank" rel="nofollow">BlueDragon</a>, with equally interesting results.</p>
<p>The Java class hierarchy behind a Railo array:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.lang.Object
	railo.runtime.type.ArrayImpl</pre></div></div>

<p>The Java class hierarchy behind a Railo structure:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.lang.Object
	railo.runtime.type.StructImpl</pre></div></div>

<p>The Java class hierarchy behind a BlueDragon array:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.lang.Object
	com.naryx.tagfusion.cfm.engine.cfData
		com.naryx.tagfusion.cfm.engine.cfJavaObjectData
			com.naryx.tagfusion.cfm.engine.cfArrayData
				com.naryx.tagfusion.cfm.engine.cfArrayListData</pre></div></div>

<p>The Java class hierarchy behind a BlueDragon structure:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.lang.Object
	com.naryx.tagfusion.cfm.engine.cfData
		com.naryx.tagfusion.cfm.engine.cfJavaObjectData
			com.naryx.tagfusion.cfm.engine.cfStructData</pre></div></div>

<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>
<p>Adendum: Ben Nadel has a good little post on <a href="http://www.bennadel.com/blog/380-ColdFusion-Data-Types-From-Different-Sources-And-How-ColdFusion-Sees-Them-.htm" title="Data Types from Different Sources and How ColdFusion Sees Them" target="_blank" rel="nofollow">ColdFusion Data Types from Different Sources and How ColdFusion Sees Them</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/the-java-behind-a-coldfusion-array-and-structure/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Inner Workings of a ColdFusion Array and Structure</title>
		<link>http://www.simonwhatley.co.uk/the-inner-workings-of-a-coldfusion-array-and-structure</link>
		<comments>http://www.simonwhatley.co.uk/the-inner-workings-of-a-coldfusion-array-and-structure#comments</comments>
		<pubDate>Tue, 11 Mar 2008 09:50:37 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[reference types]]></category>
		<category><![CDATA[struct]]></category>
		<category><![CDATA[structure]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=465</guid>
		<description><![CDATA[Array and Structures are considered to be complex data types in ColdFusion. In contrast, simple data types are ones that contain a single piece of data, such as an Integer, String, or Boolean value. A complex data type can contain multiple pieces of data, which, in the case of arrays, are usually related. All the data are referenced under a single variable name. You can think of a complex variable as a variable that contains a collection of other variables inside it. An array maps Integers to arbitrarily typed objects (Integers, Strings, Booleans and Objects) while a structure, or associative array, maps arbitrarily typed objects to arbitrarily typed objects.]]></description>
			<content:encoded><![CDATA[<p>Arrays and Structures are considered to be complex data types in ColdFusion. In contrast, simple data types are ones that contain a single piece of data, such as an Integer, String, or Boolean value. A complex data type can contain multiple pieces of data, which, in the case of arrays, are usually related. All the data are referenced under a single variable name. You can think of a complex variable as a variable that contains a collection of other variables inside it. An array maps Integers to arbitrarily typed objects (Integers, Strings, Booleans and Objects) while a structure, or associative array, maps arbitrarily typed objects to arbitrarily typed objects.</p>
<p>Arrays and structures are also known as reference types. A reference is an object containing information which refers to data stored elsewhere, as opposed to containing the data itself. References are fundamental to constructing many data structures and in exchanging information between different parts of a program. References increase flexibility in where objects can be stored, how they are allocated, and how they are passed between areas of code. As long as we can access a reference to the data, we can access the data through it, and the data itself need not be moved. They also make sharing of data between different code areas easier; each keeps a reference to it.</p>
<p>Array and structure reference types are pointers to a memory space. Pointers are the most primitive and error-prone but also one of the most powerful and efficient types of references, storing only the address of an object in memory. Because arrays and structures are pointers to a space in memory, it allows for easy copying of that data. However, when copying an array or structure, there is a distinct difference between how the copy is performed. This difference relates to the concepts of shallow and deep copying.</p>
<p>When copying an array, a deep copy is performed by ColdFusion. This means that the data is copied and a reference to the new copy is created in memory. Therefore, following the example below, a change in the value held at position 1 of array <code>b</code> will not over-write the value held in position 1 of array <code>a</code> as they have different memory references:</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;">	a <span style="color: #0000ff">=</span> <span style="color: #800080;">arrayNew</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #000000;">&#41;</span>;</span>
<span style="color: #000099;">	a<span style="color: #000000;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #0000ff">=</span> <span style="color: #ff0000;">100</span>;</span>
<span style="color: #000099;">	b <span style="color: #0000ff">=</span> a; <span style="color: #808080;">//deep copy for Array</span></span>
<span style="color: #000099;">	b<span style="color: #000000;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #0000ff">=</span> <span style="color: #ff0000;">500</span>;</span>
<span style="color: #000099;">	<span style="color: #800080;">writeOutput</span><span style="color: #000000;">&#40;</span>a<span style="color: #000000;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>; <span style="color: #808080;">// the returned result is 100</span></span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p>We can also use the little-known Java function <code>equals()</code> to demonstrate that the two arrays are not equal (the references are different).</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #808080; background-color:#ffff80;">&lt;!---</span>
<span style="color: #333333;">the following returns false</span>
<span style="color: #333333;">a and b do not refer to the same object</span>
<span style="color: #333333;"> ---&gt;</span></span>
<span style="color: #333333;"><span style="color: #800000;">&lt;cfdump</span> <span style="color: #0000ff;">var</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;#a.equals(b)#&quot;</span><span style="color: #800000;">&gt;</span></span></pre></div></div>

<p>This is not the case for a structure. When copying a structure, as in the example below, a shallow copy is performed by ColdFusion. This means that a change in b.name will also result in a change of a.name:</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;">	a <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;">	a.<span style="color: #0000ff;">name</span> <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Simon Whatley&quot;</span>;</span>
<span style="color: #000099;">	b <span style="color: #0000ff">=</span> a; <span style="color: #808080;">//shallow copy for Struct</span></span>
<span style="color: #000099;">	b.<span style="color: #0000ff;">name</span> <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;John Doe&quot;</span>;</span>
<span style="color: #000099;">	<span style="color: #800080;">writeOutput</span><span style="color: #000000;">&#40;</span>a.<span style="color: #0000ff;">name</span><span style="color: #000000;">&#41;</span>; <span style="color: #808080;">// the returned result is John Doe</span></span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p>Using the <code>equals()</code> method, we can show that the two structures refer to the same object:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #808080; background-color:#ffff80;">&lt;!---</span>
<span style="color: #333333;">the following returns true</span>
<span style="color: #333333;">a and b refer to the same object</span>
<span style="color: #333333;"> ---&gt;</span></span>
<span style="color: #333333;"><span style="color: #800000;">&lt;cfdump</span> <span style="color: #0000ff;">var</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;#a.equals(b)#&quot;</span><span style="color: #800000;">&gt;</span></span></pre></div></div>

<p>However, by using the <code>duplicate()</code> function, a deep copy of a structure can be performed:</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;">	a <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;">	a.<span style="color: #0000ff;">name</span> <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Wu Mingshi&quot;</span>;</span>
<span style="color: #000099;">	b <span style="color: #0000ff">=</span> <span style="color: #800080;">duplicate</span><span style="color: #000000;">&#40;</span>a<span style="color: #000000;">&#41;</span>; <span style="color: #808080;">//deep copy for Struct</span></span>
<span style="color: #000099;">	b.<span style="color: #0000ff;">name</span> <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Ivan Ivanovic&quot;</span>;</span>
<span style="color: #000099;">	<span style="color: #800080;">writeOutput</span><span style="color: #000000;">&#40;</span>a.<span style="color: #0000ff;">name</span><span style="color: #000000;">&#41;</span>; <span style="color: #808080;">// the returned result is Wu Mingshi</span></span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p>The above code results in two structures being created.</p>
<p>Arrays and structures although similar, behave very differently. Both are known as reference types which refer to a pointer in memory. Both have the ability to contain multiple values underneath a single variable name. Both use an index to access an individual value, but the index is numeric for arrays and a arbitrary, text value for structures. Arrays are extremely useful for numeric calculations, tabular data, and data sorting. Structures, by their nature, cannot be sorted by value, only by key name. They are commonly for related data, where order is not important and direct access to an individual element is important. Many of ColdFusion&#8217;s variable scopes (such as server, application, variables, session, form etc) can be accessed as structures.</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/the-inner-workings-of-a-coldfusion-array-and-structure/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

