Home > Software design >  Variable from Input
Variable from Input

Time:10-14

Say I have a list of four different departments

  1. Music
  2. Science
  3. PE
  4. Maths

Am I able to set it up so that when you choose the number it stores the department as a variable of $department

I want to do a Write-Host with a list of all four departments and then a $department = Read-Host asking them select a number from 1-4 and it will set the variable as Maths etc.

Any help is appreiciated!

Kind Regards

CodePudding user response:

Using enums - get you started, no error handling like above

enum Department
{
    Music = 1
    Science = 2
    PE = 3
    Maths = 4
}

$Departments = [Department]

Write-Host "Please Select"
Write-Host "-------------"
forEach ($departmentName in $Departments.GetEnumNames())
{
    Write-Host "$departmentName = $(($Departments::$departmentName).value__)"
}

$DepartmentNumber = Read-Host "Please Select a Department number"
$Department = [Department]$DepartmentNumber

Write-Host $Department

Output:

Please Select
-------------
Music = 1
Science = 2
PE = 3
Maths = 4
Please Select a Department: 3
PE

Further reading on enum: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_enum?view=powershell-7.1

CodePudding user response:

Thought Process

This is a pretty textbook problem. You want to have an array of departments, and then display the array to the user with an integer corresponding to the element's index 1.

You then receive an integer input from the user corresponding to a department (with a bit of error checking to ensure they enter an integer that actually corresponds to a department) and then use that integer to get the appropriate array element from the departments array. Since input from Read-Host is a string by default, you need to cast $UserInput as an [int] before using it to get the index of an array.

Again, since we're listing the values to the user using array index 1, we must consider that when retrieving the array element from the array, hence why we subtract 1 from the value obtained from the user.

Code

# Our array of departments
$Departments = @(
    "Music",
    "Science",
    "PE",
    "Maths"
)

# List each department for the user to pick from, being sure to off-set it's array index by 1
foreach($Department in $Departments){
    Write-Host "$([array]::IndexOf($Departments, $Department)   1). $Department"
}

# Get the appropriate value that corresponds to a department from the user, retry if user entered invalid value
do {
    $UserInput = Read-Host -Prompt "Enter the number corresponding to the desired department"
} while ($UserInput -le 0 -or $UserInput -gt $Departments.Length)

# Once we have a value from the user, use it to assign get the appropriate department, make sure to offset it by 1
$SelectedDepartment = $Departments[[int]$UserInput - 1]
Write-Host "User selected department: $SelectedDepartment"

Example Execution and Output

1. Music
2. Science
3. PE
4. Maths
Enter the number corresponding to the desired department: 0
Enter the number corresponding to the desired department: 5
Enter the number corresponding to the desired department: 4
User selected department: Maths
  • Related