The System.Collection.Stack follows the principle of last in, first out.
1
2
3
4
5
|
$mystack = new-object system.collections.stack
$myStack.Push( “Stephane” )
$myStack.Push( “van” )
$myStack.Push( “gulick” )
$myStack.Push( “district”)
|
If we check what the “$mystack” variable contains it will show us the following:
1
2
3
4
5
|
PS C:Windowssystem32> $mystack
district
gulick
van
Stephane
|
As opposed with the queue collection, wich returns the most old item in the collection (The one added first), the powershell Stack collection will return the last item you added to the collection (The most recent one). So, in other words The last item you added, will be the first one to be returned.
Use cases for the powershell system.collections.stack collection:
If you have used the powershell Stack collection before, please share with us for what use case you have used it (via the comment section below).
Methods and properties you don’t want to miss:
Lets have a look at the members of our collection object:
- push
- pop
- peek
the .push() method
As you could have noticed before, in our stack example above, we already used the push() method, and not add() (which doesn’t exists on the stack object type).
As demonstrated earlier, the push() method allows us to add a new element onto our stack. yes onto our stack, not into the stack.
The Pop() method
The pop method will give us the possibility to retrieve the item on the top of our stack. This means, that the pop() method returns the last item that has been added.
As you can see, “district” was the last item that we added, but the first one to be returned when we called the pop() method.
The Peek() method:
The peek method will work exactly as the pop() method, except, that the item that was returned will not be removed from the stack. As it’s name suggest, it allows you to peek onto the stack, and to see what would eventually be returned if you would call the pop() method.
AS you can see in the example above, the peek method (in red) returns the item, but doesn’t removes it from the powershell stack collection.
Using the pop method, returns the “gulick” item just as the peek method informed us it would do, and in this case, removed it from the stack item.
A word about the ‘count’ property:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$mystack = new-object system.collections.stack
$myStack.Push( “Stephane” )
$myStack.Push( “van” )
$myStack.Push( “gulick” )
$myStack.Push( “district”)
while($mystack.count -gt 0){
“Returning Element -> $($mystack.Peek())”
start-sleep -Seconds 3
$mystack.Pop()
}
Write-Output “End of example”
|
The powershell Stack collection allows us to go through a collection, and return each item without using a loop such as a foreach or a for statement.
Conclusion:
The second positive thing is that you can now ‘really’ have way of controlling the order in which each element will be treated. Since we know that the pop() method of the powershell stack collection returns the most young item from the collection (the last one added).
Have you used a stack collection already in one of your scripts? I’ll be curious to know how you used. Let us know!
Links
MSDN link to the Collections.Stack –> msdn
1 |
write-host "See you in the next article :)" |
Leave A Comment