<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.technet.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Find the Index Number of a Value in a PowerShell Array</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/07/find-the-index-number-of-a-value-in-a-powershell-array.aspx</link><description>In this blog post, Microsoft Scripting Guy, Ed Wilson, talks about finding the index number of a value in a Windows PowerShell array.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Find the Index Number of a Value in a PowerShell Array</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/07/find-the-index-number-of-a-value-in-a-powershell-array.aspx#3472463</link><pubDate>Thu, 22 Dec 2011 00:11:46 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3472463</guid><dc:creator>JV</dc:creator><description>&lt;p&gt;Exactly the same way. You didn&amp;#39;t read the article very closely.&lt;/p&gt;
&lt;p&gt;PS&amp;gt;$x=&amp;#39;helo&amp;#39;,&amp;#39;world&amp;#39;,&amp;#39;in&amp;#39;,&amp;#39;space&amp;#39;,&amp;#39;bees&amp;#39;,&amp;#39;knees&amp;#39;&lt;/p&gt;
&lt;p&gt;PS&amp;gt;[array]::IndexOf($x,&amp;#39;space&amp;#39;)&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3472463" width="1" height="1"&gt;</description></item><item><title>re: Find the Index Number of a Value in a PowerShell Array</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/07/find-the-index-number-of-a-value-in-a-powershell-array.aspx#3472402</link><pubDate>Wed, 21 Dec 2011 17:33:24 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3472402</guid><dc:creator>jlo</dc:creator><description>&lt;p&gt;Any thoughts on how to make it find an index if the array is not a list of numbers?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3472402" width="1" height="1"&gt;</description></item><item><title>re: Find the Index Number of a Value in a PowerShell Array</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/07/find-the-index-number-of-a-value-in-a-powershell-array.aspx#3470547</link><pubDate>Tue, 13 Dec 2011 12:07:28 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3470547</guid><dc:creator>Jlo</dc:creator><description>&lt;p&gt;Could you give a quick example of getting an index number if the array is not a set of numbers? The common example would be getting an array from a text list of pc names. &lt;/p&gt;
&lt;p&gt;Thank you for the article. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3470547" width="1" height="1"&gt;</description></item><item><title>re: Find the Index Number of a Value in a PowerShell Array</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/07/find-the-index-number-of-a-value-in-a-powershell-array.aspx#3469479</link><pubDate>Wed, 07 Dec 2011 10:03:24 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3469479</guid><dc:creator>Klaus Schulte</dc:creator><description>&lt;p&gt;Hi Ed,&lt;/p&gt;
&lt;p&gt;indexing into an array and finding the index of an array element are quite a common task that have been well explained here!&lt;/p&gt;
&lt;p&gt;But ... As usual :-) ...I have some small comments to add here ...&lt;/p&gt;
&lt;p&gt;The statement [array]::indexof($array,39) works correctly if there is at most one occurrence of the element in question. If there are more than one elements having the same value in the array ... indexof will retrieve the index of the first matching element. There are overloads of &amp;quot;IndexOf&amp;quot; available that will require to add a startindex ( and an index range) to find the next or simply another occurrence of the value we wnat to find. ( &amp;quot;LastIndexOf&amp;quot; returns the index of the last occurrence :-)&lt;/p&gt;
&lt;p&gt;To conclude: If the value is not present, -1 is returned.&lt;/p&gt;
&lt;p&gt;Another ... more or less ... &amp;quot;pholosophical&amp;quot; issue is buried in the line&lt;/p&gt;
&lt;p&gt;$array[[int]($array.GetUpperBound(0)/2)..($array.GetUpperBound(0))]&lt;/p&gt;
&lt;p&gt;I thought it is hard to read and added an additional variable &amp;quot;$high=$array.GetUpperBound(0)&amp;quot;&lt;/p&gt;
&lt;p&gt;which results in &lt;/p&gt;
&lt;p&gt;$array[[int]($high/2)..$high]&lt;/p&gt;
&lt;p&gt;and I asked myselt if this is only &amp;quot;nice&amp;quot; or if it is more than that?&lt;/p&gt;
&lt;p&gt;Actually I wanted to measure the performance and if you are using this statement inside a loop I tried to find out, if it is running faster!&lt;/p&gt;
&lt;p&gt;So I did the following&lt;/p&gt;
&lt;p&gt;PS C:\Users\Schulte&amp;gt; $array=1..10000&lt;/p&gt;
&lt;p&gt;PS C:\Users\Schulte&amp;gt; Measure-Command {for ($i=0; $i -lt 100; $i++) { for ($j=[int]($array.GetUpperBound(0)/2); $j -le ($array.GetUpperBound(0)); $j++) {} }}&lt;/p&gt;
&lt;p&gt;Days &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0&lt;/p&gt;
&lt;p&gt;Hours &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0&lt;/p&gt;
&lt;p&gt;Minutes &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0&lt;/p&gt;
&lt;p&gt;Seconds &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 1&lt;/p&gt;
&lt;p&gt;Milliseconds &amp;nbsp; &amp;nbsp; &amp;nbsp;: 790&lt;/p&gt;
&lt;p&gt;Ticks &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 17903169&lt;/p&gt;
&lt;p&gt;TotalDays &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 2,07212604166667E-05&lt;/p&gt;
&lt;p&gt;TotalHours &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0,00049731025&lt;/p&gt;
&lt;p&gt;TotalMinutes &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0,029838615&lt;/p&gt;
&lt;p&gt;TotalSeconds &amp;nbsp; &amp;nbsp; &amp;nbsp;: 1,7903169&lt;/p&gt;
&lt;p&gt;TotalMilliseconds : 1790,3169&lt;/p&gt;
&lt;p&gt;PS C:\Users\Schulte&amp;gt; $high=$array.GetUpperBound(0)&lt;/p&gt;
&lt;p&gt;PS C:\Users\Schulte&amp;gt; $high&lt;/p&gt;
&lt;p&gt;9999&lt;/p&gt;
&lt;p&gt;Now the other approach&lt;/p&gt;
&lt;p&gt;PS C:\Users\Schulte&amp;gt; Measure-command {for ($i=0; $i -lt 100; $i++) { for ($j=[int]($high/2); $j -le $high; $j++){} }}&lt;/p&gt;
&lt;p&gt;Days &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0&lt;/p&gt;
&lt;p&gt;Hours &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0&lt;/p&gt;
&lt;p&gt;Minutes &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0&lt;/p&gt;
&lt;p&gt;Seconds &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0&lt;/p&gt;
&lt;p&gt;Milliseconds &amp;nbsp; &amp;nbsp; &amp;nbsp;: 733&lt;/p&gt;
&lt;p&gt;Ticks &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 7331893&lt;/p&gt;
&lt;p&gt;TotalDays &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 8,48598726851852E-06&lt;/p&gt;
&lt;p&gt;TotalHours &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0,000203663694444444&lt;/p&gt;
&lt;p&gt;TotalMinutes &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0,0122198216666667&lt;/p&gt;
&lt;p&gt;TotalSeconds &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0,7331893&lt;/p&gt;
&lt;p&gt;TotalMilliseconds : 733,1893&lt;/p&gt;
&lt;p&gt;So it looks like &amp;quot;$array.GetUpperBound(0)&amp;quot; is permanently reevaluated inside the loop which might cause an issue!&lt;/p&gt;
&lt;p&gt;Klaus&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3469479" width="1" height="1"&gt;</description></item></channel></rss>