-
Notifications
You must be signed in to change notification settings - Fork 36
/
Convertto-NewArray.ps1
108 lines (96 loc) · 3.37 KB
/
Convertto-NewArray.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#requires -version 4
<#
.SYNOPSIS
Convert range of numbers into another range of numbers maintaining ratio.
.DESCRIPTION
Convert range of numbers into another range of numbers maintaining ratio using Maths arithmetic formula.
.PARAMETER OldArray
Prompts you for old numbers array list or range default value is from 1 to 50.
.PARAMETER NewArray
Prompts you for New numbers array list or range default value is from 100 to 200.
.PARAMETER RoundNew
This is a switch how do you want your new array list rounding or without rounding numbers
.INPUTS
list of numbers in [int[]]
.OUTPUTS
Output is on console directly with oldarray and newarray list.
.NOTES
Version: 1.0
Author: Kunal Udapi
Creation Date: 27 September 2020
Purpose/Change: New script written for some other project where charts are involved for data science.
.EXAMPLE 1
PS C:\>Convertto-NewArray.ps1
This command convert number of range from 1 to 50 to 100 to 200.
.Example 2
PS C:\>.\Convertto-NewArray.ps1 -OldArray 10, 20, 30, 40 -NewArray 1,4,7 -RoundNew
Here how the number you can use, Withe RoundNew parameter result is rounded to whole integer.
.EXAMPLE 3
PS C:\>.\Convertto-NewArray.ps1 -OldArray (1..10) -NewArray (55..60)
This is another example you can take use number array information.
#>
[CmdletBinding(SupportsShouldProcess=$True,
ConfirmImpact='Medium',
HelpURI='http://vcloud-lab.com')]
Param
(
[parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
[alias('Old')]
[int[]]$OldArray = 1..50,
[parameter(Position=1, ValueFromPipelineByPropertyName=$true)]
[alias('New')]
[int[]]$NewArray = 100..200,
[switch]$RoundNew
)
begin {
$measureOld = $OldArray | Measure-Object -Minimum -Maximum
$oldArrayMin = $measureOld.Minimum
$oldArrayMax = $measureOld.Maximum
$measureNew = $NewArray | Measure-Object -Minimum -Maximum
$newArrayMin = $measureNew.Minimum
$newArrayMax = $measureNew.Maximum
$oldRangeNumber = $oldArrayMax - $oldArrayMin
$newRangeNumber = $newArrayMax - $newArrayMin
}
process {
if ($RoundNew.IsPresent -eq $false) {
foreach ($number in $OldArray)
{
if (($number -eq 0) -or ($number -eq $oldArrayMin))
{
[pscustomobject]@{
OldValue = $number
NewValue = $newArrayMin
}
}
else
{
[pscustomobject]@{
OldValue = $number
NewValue = (($number - $oldArrayMin) * $newRangeNumber / $oldRangeNumber) + $newArrayMin
}
}
}
}
else {
foreach ($number in $OldArray)
{
if (($number -eq 0) -or ($number -eq $oldArrayMin))
{
[pscustomobject]@{
OldValue = $number
NewValue = $newArrayMin
}
}
else
{
[pscustomobject]@{
OldValue = $number
NewValue = (($number - $oldArrayMin) * $newRangeNumber / $oldRangeNumber) + $newArrayMin
}
}
}
}
}
end {
}