Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Do ... while doesn't work as exspected

$
0
0

Hi all,

I have created a small function to list arrays and select one line.

I ask in the do while routine if answer is -gt arraylength.

This works fine until answer 9, but answer 55 is returned.

I don't understand, what I'm doing wrong.

Here is my function:

#*******************************************************************************
function Listnum ($lis, $Prpt)
{      
    $answ = 99
    $list = @($lis)
    $c = @()
    $repeat = $false
    $answ = $null
    Write-Host ""
    for ($i=0; $i -lt $list.Length; $i++)
    {   $c += ("" + ($i+1) + "`t" + $list[$i])   }
    do
    {
        If($repeat)
        {   If($answ -eq 100)
            {   Write-Host "`nWrong input, not numeric" -foregroundcolor red -backgroundcolor yellow `n }
        else
            {   Write-Host "`nWrong input:" $answ -foregroundcolor red -backgroundcolor yellow `n }
        }
        Write-Host $c -Separator `n
        Write-Host ""
        $repeat = $true
        $answ = Read-Host -Prompt $Prpt
        If($answ -notmatch "\d{1,2}")
        {   $answ = 100   }
    }
    while($answ -gt $list.Length)
    return ($answ-1)
}
#**End Listnum   

$a = @("green","blue","red","yellow","white","black")

listnum $a "Please select"


Viewing all articles
Browse latest Browse all 6937

Trending Articles