简体   繁体   English

PowerShell中的嵌套foreach循环

[英]Nested foreach loop in powershell

I am trying to read the values from file abc.txt in below function我正在尝试从以下函数中的文件 abc.txt 中读取值

abc.txt abc.txt

a=abcdef123
b=ngh567
c=defh123

Below is the function:下面是函数:

function List 
    {
        Write-Output "Below are the window boxes"
        $alph = @(get-content  -Path "abc.txt" | %{$_.split('=')[0]})
        $machinelist = @(get-content  -Path "abc.txt" | %{$_.split('=')[1]})
        $counter = 0
        foreach ($mac in $machinelist) {
        foreach ($env in $alph ) {

        {
              $counter++
        write-host ""$counter": Press '"$counter"' to select "$env": $mac"
        } 
      }
    }

I want the input similar to我想要类似的输入

     1: Press '1' to select a : abcdef123
     2: Press '2' to select b : ngh567
     3: Press '3' to select c : defh123
Please make a selection:

I think we need to use nested foreach but not sure what am I doing wrong Once I make the selection for eg 1 then I want read the value like env and mac我认为我们需要使用嵌套的 foreach 但不确定我做错了什么一旦我选择了例如 1 然后我想读取像 env 和 mac 这样的值

You cross-posted this same query to another site, that I responded to you on.您将同样的问题交叉发布到另一个网站,我在该网站上回复了您。 That's fine, but make sure you alert and post back to sites you used when you find and accept an answer provided so that others can follow if they have such a use case.这很好,但请确保在您找到并接受提供的答案时提醒并回帖到您使用的网站,以便其他人在遇到此类用例时可以关注。

You also change what you said you were after and even here you are not being as clear as you were on the other site.你也改变了你所说的你所追求的东西,即使在这里你也没有你在另一个网站上那么清楚。

You are still over complicating this use case, due to the experience curve.由于经验曲线,您仍然使这个用例过于复杂。 We all have to go through that, but resolve that through resource review/training.我们都必须经历那个,但通过资源审查/培训来解决这个问题。 Guessing just leads to disappointment, errors, bad code, bad habits, etc. What I gave you on the other sites, examples and such, should have gotten you to your results, yet, here I'll give this.猜测只会导致失望、错误、糟糕的代码、不良习惯等。我在其他网站上给你的东西、例子等等,应该会让你得到你的结果,然而,在这里我会给出这个。

As for your post here.至于你在这里的帖子。 You do not need nested loops for this basic console menu effort, or all those additional split items.对于这个基本的控制台菜单工作或所有这些额外的拆分项目,您不需要嵌套循环。 You can do this all in line, this way.您可以通过这种方式在线完成所有这些操作。 Yet, note there are many ways to do X or Y, this is just one.然而,请注意有很多方法可以做 X 或 Y,这只是一种。

@'
a=abcdef123
b=ngh567
c=defh123
'@ | Out-File -FilePath 'D:\temp\abc.txt'
Get-Content -Path 'D:\temp\abc.txt'


function Start-MenuList 
{
    $counter = 0

    "Below are the window boxes`n"

     Get-Content -Path 'D:\temp\abc.txt' | 
     ForEach {
        $counter++
        "$counter : Press $counter to select $(($PSItem -split '=')[0]) : $(($PSItem -split '=')[1])"
     }

     Read-Host -Prompt "`nPlease enter a selection"
}

Clear-Host 
Start-MenuList 

<#
# Results

Below are the window boxes

1 : Press 1 to select a : abcdef123
2 : Press 2 to select b : ngh567
3 : Press 3 to select c : defh123

Please enter a selection: 
#>

There is no need for nesting loops in your case;在您的情况下不需要嵌套循环 it's sufficient to create a nested array , ie an array of subarrays of two elements each:创建嵌套数组就足够了,即每个包含两个元素的子数组的数组:

# Get the array of value pairs from the file.
# Each array element will be a subarray of 2 elements, namely
# the tokens before and after the "=".
$valuePairs = (Get-Content abc.txt).ForEach({ , ($_ -split '=') })

# Display the menu options, one for each pair.
$i = 0; $valuePairs.ForEach({ ++$i; "${i}: $($_[0]): $($_[1])" })

# Prompt the user to choose one option.
do {
  try { [int] $chosenNdx = Read-Host 'Enter an index to select' } catch { }
} while (-not ($chosenNdx -in 1..($valuePairs.Count)))

# Display the chosen value pair:
--$chosenNdx # Entry was 1-based, subtract 1.
"You chose: $($valuePairs[$chosenNdx][0]): $($valuePairs[$chosenNdx][1])" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM