Wednesday 27 August 2014

PowerShell GetEnumerator

I was working with some PowerShell collections recently and forgot how to iterate over an object of the type System.Collections.ArrayList. So after a little digging around, I made some notes to remind myself.

The test code I used to create and populate the array was:


$myArray = New-Object -TypeName System.Collections.ArrayList(100);

$myArray.Add('hearts') | Out-Null;

$myArray.Add('clubs') | Out-Null;
$myArray.Add('diamonds') | Out-Null;
$myArray.Add('spades') | Out-Null;

Write-Host "Before trimming, capacity of the array: $($myArray.Capacity)";

$myArray.TrimToSize();
$myArray.Sort();
Write-Host "After trimming, capacity of the array: $($myArray.Capacity)";
Write-Host "Number of elements actually contained in the ArrayList: $($myArray.Count)";


I created myArray variable with a capacity of 100.  I didn't have to of course as I've only used the suit names found in a deck of playing cards, i.e., four names. This number is so trivial I could have created a new instance of the ArrayList class that is empty and has the default initial capacity. I'll mention later what the default initial capacity might be.

The return type of the ArrayList.Add Method is the type System.Int32, a 32-bit signed integer. Rather than see this value (which represents the ArrayList index at which the value has been added) I've suppressed the output by using the Out-Null Cmdlet. The Out-Null cmdlet sends output to NULL, in effect, deleting it.

Other ArrayList methods and properties have been used to give a little information regarding the array.

Property Capacity - in the first use of this property, the value returned is 100 which is the same value as the number of elements (capacity) used as an argument in the constructor.

In the second use of this property, we get a value of four which reflects the number of suits in our deck of cards. The value has changed because of the use of the TrimToSize method.

Method TrimToSize - sets the capacity to the actual number of elements in the ArrayList. In our example, four. Of course, we could have used a value four in our constructor knowing what we were going to add to the arraylist. But this is just to show what can be done. Especially if you might not know what the number of items you're going to load into the arraylist will be. It's easy to specify the correct value in small demonstrations like this.

Method Sort - sort the elements in the arraylist.

Property Count - confirms the number of elements that are actually in the ArrayList.

Enumerating over the collection - example 1


$myenum = $myArray.GetEnumerator();
while($myenum.MoveNext())
{
    Write-Host "Our playing card is now: $($myenum.Current)";
}


The property $myenum.Current represents the current element in the collection.


For details of the methods and properties of variable myenum, have a look at the IEnumerator interface.


Enumerating over the collection - example 2


$myArray.GetEnumerator() | ForEach-Object {
   Write-Host "Our playing card is now: $_";
}



Keywords: enum enumerator iterate

No comments:

Post a Comment