convert “Yes” or “No” to boolean

流过昼夜 提交于 2019-12-22 10:36:50

问题


I want to parse user values contained in .CSV file. I don't want my users to enter "Yes" or "No" but instead enter "True" or "False". In each case I want to convert to the equivalent boolean values: $true or $false. Ideally I would like a default value, so if there's misspelt "Yes or "No" I would return my default value: $true or $false.

Hence, I wondered if there is a neat way of doing this other than

if(){} else (){}

回答1:


One way is a switch statement:

$bool = switch ($string) {
  'yes' { $true }
  'no'  { $false }
}

Add a clause default if you want to handle values that are neither "yes" nor "no":

$bool = switch ($string) {
  'yes'   { $true }
  'no'    { $false }
  default { 'neither yes nor no' }
}

Another option might be a simple comparison:

$string -eq 'yes'            # matches just "yes"

or

$string -match '^y(es)?$'    # matches "y" or "yes"

These expressions would evaluate to $true if the string is matched, otherwise to $false.




回答2:


Ah, the magic of powershell functions, and invoke expression.

function Yes { $true }
function No { $false }

$magicBool = & $answer 

Note: This is case insensitive, but will not handle misspellings




回答3:


If the only possible values are "Yes" and "No" then probably the simplest way is

$result = $value -eq 'Yes'

With misspelled values and the default $false the above will do as well.

With misspelled values and the default $true this will work

$result = $value -ne 'No'



回答4:


All of these are valid approaches. If you are looking for a one liner, this will validate it is an acceptable value and set to boolean true if in the 'true' value set. This will also give you a default $false value.

$result = @("true","false","yes","no") -contains $value -and @("true","yes") -contains $value

For a default $true value you would need something like so.

$result = $true

if (@("true","false","yes","no") -contains $value) {
    $result = @("true","yes") -contains $value
}



回答5:


Without a full snippet of your existing code, something like this would probably be an alternative path to take, as opposed to a string of IF statements.

NOTE: This will not handle simple 'Y' or 'N' input, but is case insensitive. So, you should be able to see 'yes' or 'YES' working, as well.

$myVar = Read-Host 'What is your answer?'
switch ($myVar)
{
  Yes {$myVarConverted = $true; break}
  True {$myVarConverted = $true; break}
  No {$myVarConverted = $false; break}
  False {$myVarConverted = $false; break}
  default {"Invalid Input"; break}
}
Write-Host $myVarConverted

Please see my additional comment on your question about the 'misspelling' caveat. That's difficult to code around without specific restrictions or requirements.




回答6:


Here's the way I do Yes-No answers:

function ask-user
{
   [CmdletBinding()]
   Param(
      [Parameter(Mandatory=$true)]
      [string] $question
   )
   Process
   {  $answer = read-Host $question
      if ("yes" -match $answer) {$true}
      elseif ("no" -match $answer) {$false}
      else {ask-user $question}
   }
}

You can easily substitute true and false for yes and no.
This one is case insensitive, and will match valid abbreviations. (Y or N). In the case of misspellings, it asks again. Yeah, I could have done it without recursion, but I'm lazy.




回答7:


These are great solutions above, but let me just say that this whole topic just proves the vast shortcomings of Powershell...

  [System.Convert]::ToBoolean("False") -eq $true ? 
  [System.Convert]::ToBoolean("0") -eq $true ?  

Really? Give me a f--kin break.



来源:https://stackoverflow.com/questions/36290135/convert-yes-or-no-to-boolean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!