Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Part of the problem is how the array is being used inside the function. Remember, a function in PowerShell doesn't actually <em>return</em> anything. It writes objects to the pipeline. Therefore, the return is superfluous, but not actually causing any problems. The use of the <code>Add</code> function is causing the problem because <code>Add</code> <a href="http://msdn.microsoft.com/en-us/library/system.collections.arraylist.add.aspx" rel="nofollow noreferrer">returns</a> the index at which the value was added and therefore writes to the pipeline as well.</p> <pre><code>function get-myarray { $al = New-Object System.Collections.ArrayList $al.Add( 0 ) $al.Add( 1 ) $al.Add( 'me@co.com' ) $al.Add( 'you.co.com' ) return $al } $array = get-myarray $array.Count 8 </code></pre> <p>Note how the size is 8. What needs to be done is to suppress the writing of what is returned by the <code>Add</code> function. There are a few ways to do this but here is one:</p> <pre><code>function get-myarray { $al = New-Object System.Collections.ArrayList $al.Add( 0 ) | out-null $al.Add( 1 ) | out-null $al.Add( 'me@co.com' ) | out-null $al.Add( 'you.co.com' ) | out-null return $al } $array = get-myarray $array.Count 4 </code></pre> <p>I don't believe the use of `ArrayList' is a wrong one if you want to remove items from it. </p> <p>As far as <em>unrolling</em> goes, this deserves a whole other question and has been <a href="https://stackoverflow.com/a/1829401/4916">already addressed</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload