Reputation: 1913
I am trying to read the values from file abc.txt in below function
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 e.g. 1 then I want read the value like env and mac
Upvotes: 1
Views: 1334
Reputation: 440337
There is no need for nesting loops in your case; it's sufficient to create a nested array, i.e. 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])"
Upvotes: 0
Reputation: 16116
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.
@'
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:
#>
Upvotes: 1