<?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>rabidGadfly &#187; array</title>
	<atom:link href="http://rabidgadfly.com/category/array/feed/" rel="self" type="application/rss+xml" />
	<link>http://rabidgadfly.com</link>
	<description>Simple Solutions to Nagging Coding Problems</description>
	<lastBuildDate>Wed, 19 Oct 2011 13:43:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Query Array Prototype</title>
		<link>http://rabidgadfly.com/2006/12/query-array-prototype/</link>
		<comments>http://rabidgadfly.com/2006/12/query-array-prototype/#comments</comments>
		<pubDate>Thu, 14 Dec 2006 17:27:30 +0000</pubDate>
		<dc:creator>rabidgadfly</dc:creator>
				<category><![CDATA[array]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://www.rabidgadfly.com/?p=14</guid>
		<description><![CDATA[I recently found myself in need of a method to search values in a multidimensional array. I searched around a bit and could only find methods that searched the entire array for a specific value. This doesn&#8217;t work for me, however, because a particular value may appear in several places within my array. I needed [...]]]></description>
			<content:encoded><![CDATA[<p>I recently found myself in need of a method to search values in a multidimensional array. I searched around a bit and could only find methods that searched the entire array for a specific value. This doesn&#8217;t work for me, however, because a particular value may appear in several places within my array. I needed a way to look for a value in a specific positional within the array, so I ended up creating the following prototype:</p>
<blockquote><p>
Array.prototype.queryArray = function(columnName:String,columnValue:String):Number {<br />
&nbsp;var arrLength:Number = this.length;<br />
&nbsp;while(arrLength&#8211;)      {<br />
&nbsp;&nbsp;if(this[arrLength][columnName] == columnValue){<br />
&nbsp;&nbsp;&nbsp;return arrLength;<br />
&nbsp;&nbsp;};<br />
&nbsp;};<br />
&nbsp;return -1;<br />
};
</p></blockquote>
<p>In order to use the solution I came up with, you also have to create your array the way I do. I assign a name to each element within each &#8216;record&#8217; which turns it into a table of sorts. For example:</p>
<blockquote><p>var my_array:Array = new Array();<br />
my_array.push({firstname:&#8221;joe&#8221;,lastname:&#8221;shmoe&#8221;,phone:&#8221;5558118&#8243;});<br />
my_array.push({firstname:&#8221;jane&#8221;,lastname:&#8221;doe&#8221;,phone:&#8221;5558888&#8243;});</p></blockquote>
<p>The prototype looks for a column name and a value to search for within that column. If a match is found the array index is returned. If no match is found, -1 is returned. Need some examples?</p>
<blockquote><p>var searchResult:Number = my_array.queryArray(&#8220;phone&#8221;,&#8221;5558888&#8243;);<br />
trace(searchResult); //returns 1</p>
<p>searchResult = my_array.queryArray(&#8220;firstname&#8221;,&#8221;joe&#8221;);<br />
trace(searchResult); //returns 0</p>
<p>searchResult = my_array.queryArray(&#8220;firstname&#8221;,&#8221;sally&#8221;);<br />
trace(searchResult); //returns -1
</p></blockquote>
<p>Using this prototype I can check to see if a &#8216;record&#8217; exists for a particular object. If it exists, I can update that record with new values or delete it from the array. If it doesn&#8217;t exist, I can push it.</p>
<p>-rG</p>
]]></content:encoded>
			<wfw:commentRss>http://rabidgadfly.com/2006/12/query-array-prototype/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

