PoshCode Archive  Artifact Content

Artifact fc98ddaf9e7eae6db9f42df4ce0c3884a88e371382c69caf7e06a5c8ba45316c:

  • File Skip-Object.ps1 — part of check-in [eb5ebf5241] at 2018-06-10 13:01:07 on branch trunk — Skip -every 3rd, or -upto 3 at a time, or the -first 3, or the -last 3 or any combination of the above … (user: Joel Bennett size: 1042)

# encoding: ascii
# api: powershell
# title: Skip-Object
# description: Skip -every 3rd, or -upto 3 at a time, or the -first 3, or the -last 3 or any combination of the above …
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Skip-Object
# x-poshcode-id: 1778
# x-archived: 2010-04-17T07:56:02
#
#
function Skip-Object {
param( 
   [int]$First = 0, [int]$Last = 0, [int]$Every = 0, [int]$UpTo = 0,  
   [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
   $InputObject
)
begin {
   if($Last) {
      $Queue = new-object System.Collections.Queue $Last
   }
   $e = $every; $UpTo++; $u = 0
}
process {
   $InputObject | where { --$First -lt 0 } | 
   foreach {
      if($Last) {
         $Queue.EnQueue($_)
         if($Queue.Count -gt $Last) { $Queue.DeQueue() }
      } else { $_ }
   } |
   foreach { 
      if(!$UpTo) { $_ } elseif( --$u -le 0) {  $_; $U = $UpTo }
   } |
   foreach { 
      if($every -and (--$e -le 0)) {  $e = $every  } else { $_ } 
   }
}
}