Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 用户输入前数组中的所有元素_Powershell - Fatal编程技术网

Powershell 用户输入前数组中的所有元素

Powershell 用户输入前数组中的所有元素,powershell,Powershell,我有一个脚本,要求用户输入。只有五个值是可能的,但我想以某种方式将他的输入与我的硬编码数组进行比较,并得到它之前的所有元素 $checkArray = @("one","two","three","four","five") 例如,他的输入将是“三”,然后我希望数组变成: $array = @("one","two","three") 编辑: 到目前为止,我有: $userinput = "three" $checkArray = @("one","two","three","four","

我有一个脚本,要求用户输入。只有五个值是可能的,但我想以某种方式将他的输入与我的硬编码数组进行比较,并得到它之前的所有元素

$checkArray = @("one","two","three","four","five")
例如,他的输入将是“三”,然后我希望数组变成:

$array = @("one","two","three")
编辑: 到目前为止,我有:

$userinput = "three"
$checkArray = @("one","two","three","four","five")
$position = $checkArray.IndexOf($userinput)
$length = $checkArray.Length
$newarray = $checkArray | Select -First $($length-$position)

实际上不需要数组的长度,只需要位置。将+1添加到位置(数组从0开始计数),所有操作都应该正常

$userinput = "four"
$checkArray = @("one","two","three","four","five")
$position = $checkArray.IndexOf($userinput)
$newarray = $checkArray | Select -First ($position + 1)
$newarray

实际上不需要数组的长度,只需要位置。将+1添加到位置(数组从0开始计数),所有操作都应该正常

$userinput = "four"
$checkArray = @("one","two","three","four","five")
$position = $checkArray.IndexOf($userinput)
$newarray = $checkArray | Select -First ($position + 1)
$newarray

欢迎来到StackOverflow。请告诉我们你到目前为止做了什么。查看并尝试提供您的问题和代码的详细信息。我已经添加了到目前为止的内容,很抱歉:)您的代码似乎正常工作,到底是什么问题?如果用户输入为“四”,则无法正常工作,我的新数组将变为“一,二”,而不是“一,二,三,四”。如果用户输入是“2”,我得到的是“1,2,3,4”,而不是“1,2”。欢迎使用StackOverflow。请告诉我们你到目前为止做了什么。查看并尝试提供您的问题和代码的详细信息。我已经添加了到目前为止的内容,很抱歉:)您的代码似乎正常工作,到底是什么问题?如果用户输入为“四”,则无法正常工作,我的新数组将变为“一,二”,而不是“一,二,三,四”。如果用户输入是“二”,我得到的是“一,二,三,四”,而不是“一,二”。