Skip to content

Commit

Permalink
Add Split-Object Sample
Browse files Browse the repository at this point in the history
To split object into requires groupnumber
  • Loading branch information
guitarrapc committed Jun 12, 2013
1 parent d01be9e commit 7095073
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Split-Object/Select_skip.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$numbers = 1..131
$GroupNum = 20
$skipcount = [math]::Truncate($numbers.count / $GroupNum)

0..$skipcount | %{
$numbers | Select-Object -skip $($GroupNum*$_) -First $GroupNum
"--------------------------------------------"
}


53 changes: 53 additions & 0 deletions Split-Object/Split-Object.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function Split-Object{

[CmdletBinding(
SupportsShouldProcess = $false,
ConfirmImpact = "none",
DefaultParameterSetName = ""
)]

param
(
[Parameter(
HelpMessage = "Input Object you want to split",
Position = 0,
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
#[ValidateNotNullOrEmpty()]
[object[]]
$Item,

[Parameter(
HelpMessage = "Input Number you want to split each",
Position = 0,
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[ValidateNotNullOrEmpty()]
[int]
$GroupNumber
)

begin
{
$skipcount = [math]::Truncate($Item.count / $GroupNumber)
}

process
{

0..$skipcount | %{
$Item | Select-Object -skip $($GroupNumber * $_) -First $GroupNumber
# {Set command you want to do...}
}
}

end
{
}
}

Split-Object -Item $(ps | sort WS -Descending) -GroupNumber 20

0 comments on commit 7095073

Please sign in to comment.