PoshCode Archive  Artifact [fcb4abdb0e]

Artifact fcb4abdb0e7b0ef25272426db3b3a1d402e7e3b2838914d478c814e7056ca43a:

  • File Skip-Object.ps1 — part of check-in [84b4f6e2c6] at 2018-06-10 13:00:53 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: unknown size: 1054)

# 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
# license: CC0
# function: Skip-Object
# x-poshcode-id: 1749
# x-archived: 2010-04-11T23:18:24
#
#
function Skip-Object {
param( 
   [int]$First = 0
, 
   [int]$Last = 0
,
   [int]$Every = 0
,
   [int]$UpTo = 0
)
begin {
   if($Last) {
      $Queue = new-object System.Collections.Queue $Last
   }
   $e = $every
   $UpTo++
   # $u = $UpTo
}
process {
   $_ | 
   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 { $_ } 
   }
}
}