Home > Net >  Powershell Loop that will assign a value to a variable once the number chosen by a client is reached
Powershell Loop that will assign a value to a variable once the number chosen by a client is reached

Time:03-08

There's 2 things I'm trying to get accomplished.

  1. I'm attempting to list all the values of an array to a user while having that list numbered.
  2. I'm attempting to assign a value to a variable, depending on what the user chooses.
# List that prints array items for a user to choose from
$Array = @('A', 'B', 'C', 'D', 'E', 'F')

foreach($i in $Array)
    {
        Write-Host "|" $i #this | character should be a list from 1-6, listing all the groups in the array
        }
    Write-Output "Please enter which group:" 
    $Array = Read-Host -Prompt " "

# Assign value to the variable
$x = Read-Host "Please enter a number"

$Array = @('A', 'B', 'C', 'D', 'E', 'F')

$i=0
while ($i -lt $x) {
    $i  
    if ($i -eq $x) { continue }
}
$x = $Array[$i]

Write-Host "You're assigned to group " $x

I've figured this dirty way of doing it, but I was trying to see if there was a simpler way of doing this (someone mentioned multidimmensional arrays). The 2 pieces of code should work together, explaining the same variable assignment.

Thanks!

EDIT 1: I want the user to not have to input "0" for the A group.

CodePudding user response:

I think I understood you correctly...

In this situation, I would use a for loop as it's meant for these kind of scenarios:

$Array = @('A', 'B', 'C', 'D', 'E', 'F')
for ($i = 0; $i -lt $Array.Count; $i  )
{
    $j = $i   1
    "{0}: {1}" -f $j, $Array[$i] 
}

$selection = (Read-Host -Prompt "Enter Number(s)").Split(',').Trim()
foreach ($selected in $selection)
{
    $Array[$selected - 1]
}
#Outputs:
<#
1: A
2: B
3: C
4: D
5: E
6: F
Enter Number(s): 1, 2, 3
A
B
C
#>

By adding 1 to $i (and assigning it to $j), we start at 1 for our numerical index assignment to the current iteration in your $Array. This way, we could still use $i to refer to the actual index value and have it display along side it. Using the -f format operator, this allows for "cleaner" code; where $j is 1, and $Array[$i] is 0 so A.

Finally, using a foreach loop, we can make a multiple choice selection if you pass more than one number separated by a comma. The only thing that's left to do is subtract 1 from the selected number(s): $Array[$selected - 1], and it will give you the correct value in your array.

CodePudding user response:

To complement Abraham's helpful answer with a very similar approach but implementing input validation. This one does not accept multiple inputs.

$Array = 'A', 'B', 'C', 'D', 'E', 'F'
$index = 1
foreach($item in $Array) {
    "{0}. $item" -f $index  
}
while($true) {
    $choice = Read-Host "Please enter which Group"
    if($choice -notmatch "^[1-9]\d*$" -or -not ($value = $Array[$choice-1])) {
        "Incorrect choice, no element with index $choice"
        continue
    }
    break
}

"Choice was $value"
  • Related