Monday 21 July 2008

Get-Stack2.ps1

<#
.SYNOPSIS
    This script shows the use of a stack.
.DESCRIPTION
    This script uses the system.collections.stack
    object.
.NOTES
    File Name  : Get-Stack2.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell Version 2.0
.LINK
    This script posted to:
        http://pshscripts.blogspot.co.uk/2008/07/get-stack2.html
.EXAMPLE
    Psh c:\ .\Get-Stack2.ps1
    Stack as start:
    fox
    brown
    quick
    The

    (Pop)		fox
    Stack value after Pop:
    brown
    quick
    The

    (Pop)		brown
    Stack values after 2nd pop:
    quick
    The

    (Peek)		quick
    Stack values after a peek:
    quick
    The  
#>

# Create and initialise a new stack object
$mystack = new-object system.collections.stack
$myStack.Push( 'The' )
$myStack.Push( 'quick' )
$myStack.Push( 'brown' )
$myStack.Push( 'fox' )

# Display the Stack
'Stack as start:'
$myStack
''# Pop an element from the Stack.
"(Pop)`t`t{0}" -f $myStack.Pop()
'Stack value after Pop:'
$myStack
''

# Pop another element from the Stack
"(Pop)`t`t{0}" -f $myStack.Pop()

# Display the Stack after 2nd pop
'Stack values after 2nd pop:' 
$myStack
''

# Peek at the front
"(Peek)`t`t{0}" -f $myStack.peek()

# Display the Stack after the peek
'Stack values after a peek:' 
$myStack

No comments: