freakbn
freakbn

Reputation: 27

How to get range between 0 and a number with Powershell?

From a number (163 - 1 ) and a scope (20), I try to get ranges (0..19, 20..39, ..., 160..162)

I can get all exept 160..162

$Counter = 163 -1
$Scope = "20"
$Modulo = $Counter % $Scope
$NbrLoop = [Math]::Ceiling($Counter / $Scope)

$j = 0

For($i=0; $i -le $Counter;$i++)
{
    If($i % $Scope -eq 0 -and $i -ne 0)
    {
        $k = $i
        $j..--$k
        $j = $i
        ""
    }
}

Could you please let me know how to proceed to get last range (160,161,162) ?

Upvotes: 0

Views: 904

Answers (1)

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19335

modify condition in if to accept last iteration

If($i % $Scope -eq 0 -and $i -ne 0 -or $i -eq $Counter)

however --$k must not be done int that case

$k = If ( $i -eq $Counter ) { $i } else { $i-1 }
$j..$k

Otherwise the number of iterations can be reduced

For($i=0; $i -lt $NbrLoop;$i++)
{
   $j = $i * $scope;
   $k = If( $i -eq $NbrLoop -1 ) { $Counter } else { ($i+1)*$scope -1 }; 
   $j..$k
   ""
}

Upvotes: 2

Related Questions