0

I am trying to find a way to accept user to invoke multiple switch statements for example

$order = Read-Host "Place Order"

Switch ($order){
 1{echo "Burger"}
 2{echo "Fries"}
 3{echo "Drink"}
}

You can only type 1, 2, or 3 but if you wanted multiple you could put it into an array

$order = @('1','2')

Switch ($order){
 1{echo "Burger"}
 2{echo "Fries"}
 3{echo "Drink"}
}

output:
Burger
Fries

But how do I get user input and format it into an array value?

1 Answer 1

1

Once simple solution is to ask the user to specify their order separated by commas:

$order = Read-Host "Place Order (use commas to separate items)"

Switch ($order.Split(',')){
 1{echo "Burger"}
 2{echo "Fries"}
 3{echo "Drink"}
}

So for this input:

Place Order (use commas to separate items): 1,3

I get this output:

Burger
Drink

This still works of they only specify a single item, or none at all - you probably need a default option for no input.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! This is exactly what I was looking for thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.