0而不是PowerShell中的[int]?

时间:2022-09-08 13:26:37

My understanding is that (mathematically) 0 is an Int. And yet

我的理解是(数学上)0是Int。但是

if (0 -as [int]){"Int"}else{"Not"}

returns Not, at least for me in PS 2.0.

返回不,至少对我来说PS 2.0。

Is this a bug in v2, or am I misunderstanding things? I have worked around the issue by testing for -as [int] -and -ne 0, but that is rather kludgy feeling, so I am hoping I am missing something here and there is a better answer.

这是v2中的错误,还是我误解了事情?我通过测试-as [int] -and -ne 0解决了这个问题,但这是一种愚蠢的感觉,所以我希望我在这里遗漏了一些东西并且有一个更好的答案。

FYI, I am specifically dealing with a seed computer name that needs an initial index added or a last computer name that needs an index incremented. The seed looks like Lab 1-, and the initial index could be 0 or could be 1 depending on the situation. I had been just testing for the last character to be [int], knowing that - would not be an int. But when the first computer was Lab 1-00 because the initial index was 0, I also get not [int] and things get ugly, with the next computer being Lab 1-0000. Again, the additional conditional is working, just wondering if I am misunderstanding in my expectation that 0 is an [int].

仅供参考,我专门处理需要添加初始索引的种子计算机名称或需要增加索引的最后一个计算机名称。种子看起来像Lab 1-,初始索引可以是0或者可以是1,具体取决于具体情况。我一直在测试最后一个字符是[int],知道 - 不会是一个int。但是当第一台计算机是实验室1-00因为初始索引是0时,我也没有[int]而且事情变得很难看,下一台计算机是Lab 1-0000。再次,额外的条件是有效的,只是想知道我是否误解我的期望0是[int]。

3 个解决方案

#1


The IF test is performed by invoking whatever expression is in side the parens, and then evaluating the result as [bool].

通过调用parens中的任何表达式,然后将结果评估为[bool]来执行IF测试。

0 -as [int] 

returns 0. When an [int] is evaluated as [bool] (true/false) 0 is $false, so the test always fails.

返回0.当[int]被评估为[bool](true / false)时,0为$ false,因此测试总是失败。

From the description of the problem, it sounds like you might actually be testing a string instead of an [int].

从问题的描述来看,听起来你实际上可能正在测试字符串而不是[int]。

  if ($var -as [int] -is [int]) {"Int"} else {"Not"}

Should perform that test without throwing an exception.

应该执行该测试而不抛出异常。

#2


Yes, 0 is of type [int]. You can see this for yourself using the GetType method:

是的,0的类型为[int]。您可以使用GetType方法自己查看:

PS > (0).GetType()    
IsPublic IsSerial Name                                     BaseType                  
-------- -------- ----                                     --------                  
True     True     Int32                                    System.ValueType          


PS > 

The problem is that you are using the wrong operator. You use -is to test type, not -as:

问题是您使用的是错误的运算符。您使用-is来测试类型,而不是-as:

PS > 0 -is [int]
True
PS > if (0 -is [int]) {"Int"} else {"Not"}
Int
PS > 

#3


0 -as [int] simply means cast 0 as an int. The result of the expression is still 0, which implicitly converts to false.

0 -as [int]简单地表示将0转换为int。表达式的结果仍为0,隐式转换为false。

Instead you want to use 0 -is [int], which means is 0 an int and would evaluate to true.

相反,你想使用0 -is [int],这意味着0是一个int并且将评估为true。

Further reading: get-help about_Type_Operators

进一步阅读:get-help about_Type_Operators

EDIT:

Per your comments below, here is an example of how you might evaluate if the final character can be converted to an int without throwing an exception:

根据下面的评论,下面是一个示例,说明如何在不抛出异常的情况下将最终字符转换为int:

function CheckLastChar($string){
    $var = 0
    $string -match ".$" | Out-Null

    if ([System.Int32]::TryParse($matches[0], [ref]$var)) {
        "It's an int!"
    }
    else {
        "It's NOT an int!"
    }
}
PS C:\> CheckLastChar("Lab 1-")
It's NOT an int!

PS C:\> CheckLastChar("Lab 1-000")
It's an int!

Although, I have to say mjolinor's -as [int] -is [int] solution is much nicer.

虽然,我不得不说mjolinor的-as [int] -is [int]解决方案要好得多。

#1


The IF test is performed by invoking whatever expression is in side the parens, and then evaluating the result as [bool].

通过调用parens中的任何表达式,然后将结果评估为[bool]来执行IF测试。

0 -as [int] 

returns 0. When an [int] is evaluated as [bool] (true/false) 0 is $false, so the test always fails.

返回0.当[int]被评估为[bool](true / false)时,0为$ false,因此测试总是失败。

From the description of the problem, it sounds like you might actually be testing a string instead of an [int].

从问题的描述来看,听起来你实际上可能正在测试字符串而不是[int]。

  if ($var -as [int] -is [int]) {"Int"} else {"Not"}

Should perform that test without throwing an exception.

应该执行该测试而不抛出异常。

#2


Yes, 0 is of type [int]. You can see this for yourself using the GetType method:

是的,0的类型为[int]。您可以使用GetType方法自己查看:

PS > (0).GetType()    
IsPublic IsSerial Name                                     BaseType                  
-------- -------- ----                                     --------                  
True     True     Int32                                    System.ValueType          


PS > 

The problem is that you are using the wrong operator. You use -is to test type, not -as:

问题是您使用的是错误的运算符。您使用-is来测试类型,而不是-as:

PS > 0 -is [int]
True
PS > if (0 -is [int]) {"Int"} else {"Not"}
Int
PS > 

#3


0 -as [int] simply means cast 0 as an int. The result of the expression is still 0, which implicitly converts to false.

0 -as [int]简单地表示将0转换为int。表达式的结果仍为0,隐式转换为false。

Instead you want to use 0 -is [int], which means is 0 an int and would evaluate to true.

相反,你想使用0 -is [int],这意味着0是一个int并且将评估为true。

Further reading: get-help about_Type_Operators

进一步阅读:get-help about_Type_Operators

EDIT:

Per your comments below, here is an example of how you might evaluate if the final character can be converted to an int without throwing an exception:

根据下面的评论,下面是一个示例,说明如何在不抛出异常的情况下将最终字符转换为int:

function CheckLastChar($string){
    $var = 0
    $string -match ".$" | Out-Null

    if ([System.Int32]::TryParse($matches[0], [ref]$var)) {
        "It's an int!"
    }
    else {
        "It's NOT an int!"
    }
}
PS C:\> CheckLastChar("Lab 1-")
It's NOT an int!

PS C:\> CheckLastChar("Lab 1-000")
It's an int!

Although, I have to say mjolinor's -as [int] -is [int] solution is much nicer.

虽然,我不得不说mjolinor的-as [int] -is [int]解决方案要好得多。