Skip to content

Instantly share code, notes, and snippets.

@awsr
Last active April 18, 2021 17:49
Show Gist options
  • Save awsr/123f70334257c6532b8553819bc1ff7c to your computer and use it in GitHub Desktop.
Save awsr/123f70334257c6532b8553819bc1ff7c to your computer and use it in GitHub Desktop.
Quick way to type in a lot of coordinates
$coords = @()
while ($true) {
$x = Read-Host "x"
$y = Read-Host "y"
if (-not $x -and -not $y) {
Write-Host "----------"
break
}
elseif (-not $x -or -not $y) {
Write-Warning "Missing value"
}
else {
try {
$obj = [pscustomobject]@{
x = [int]::Parse($x)
y = [int]::Parse($y)
}
Write-Host -ForegroundColor Green "--> [$($coords.Length)]"
$coords += $obj
}
catch [FormatException] {
Write-Warning "Invalid number string"
}
}
}
# ================================================
# Alternative as a slightly more advanced function
function New-CoordinatesArray {
[CmdletBinding()]
param (
[Parameter()]
[switch]$FloatingPoint
)
process {
Write-Host "Leave X and Y empty to finish"
Write-Host -NoNewLine "Configured for: "
if ($FloatingPoint) {
Write-Host "Floating Point`n"
}
else {
Write-Host "Integers`n"
}
$array = @()
while ($true) {
$x = Read-Host "x"
$y = Read-Host "y"
if (-not $x -and -not $y) {
Write-Host "----------"
break
}
elseif (-not $x -or -not $y) {
Write-Warning "Missing value"
}
else {
try {
if ($FloatingPoint) {
$obj = [pscustomobject]@{
x = [double]::Parse($x)
y = [double]::Parse($y)
}
}
else {
$obj = [pscustomobject]@{
x = [int]::Parse($x)
y = [int]::Parse($y)
}
}
Write-Host -ForegroundColor Green "--> [$($array.Length)]"
$array += $obj
}
catch [FormatException] {
Write-Warning "Invalid number string"
}
}
}
return $array
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment