转换VBScript的Eqv运算符 [英] Converting VBScript's Eqv Operator

查看:148
本文介绍了转换VBScript的Eqv运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您通过Google搜索 PowerShell Eqv ,则当前会找到 VBScript到Windows PowerShell转换指南.但是答案是很多技术帮助:

If you google for PowerShell Eqv you currently will find the VBScript-to-Windows PowerShell Conversion Guide on top of the list. Yet the answer is much of technical help:

定义:对两个表达式执行逻辑等效.
毫无疑问,Eqv有其用途;我们只是不确定它是否有实际用途.尽管可能有等效的Windows PowerShell,但我们必须诚实地说:我们看上去并不难.

Definition: Performs a logical equivalence on two expressions.
No doubt Eqv has its uses; we’re just not sure if it has any practical uses. Although there might be a Windows PowerShell equivalent we have to be honest: we didn’t look real hard for one.

实际上,我在VBScript中多次使用了Eqv运算符(也写为:A↔B),并且如果存在,可能会在PowerShell中使用它.

In fact, I used the Eqv operator (also written as: A↔B) a number of times in VBScript and likely would have used it in PowerShell if it existed.

我有一个组列表("HR", "SAP", "!IT", "...").如果用户是所有列出的组的成员,并且不是明显带有升级标记的组的成员(例如,"!IT",未将其分解为:$Negate = $True$Group = "IT"),则应该执行特定任务.该脚本需要遍历各个组,并在不满足组条件时立即中断迭代(以节省时间).
用于此的命令应该是这样的:

I have a list of groups ("HR", "SAP", "!IT", "..."). If a user is a member of all the listed groups and explicitly not a member of groups that are preceded with an escalation mark (like "!IT", which is unraveled to: $Negate = $True and $Group = "IT") a specific task should be done. The script needs to iterate through the groups and immediately break the iteration when a group condition is not met (to save time).
A command for this would have been something like:

If ($Negate -eqv (IsMember($Group))) {Break}

如何用最少的代码构建一个逻辑等价运算符?

How can I build a logical equivalence operator with a minimum of code?

推荐答案

如果从字面意义上使定义安静,那么您可能已经发现实现逻辑等效操作的可能方法:

If you take definition quiet literally, you will probably already see a possible way to achieve a logical equivalence operation:

If ([Bool]$Expression1 -eq [Bool]$Expression2) {...}

但是,如果仔细查看真值表,您可能会发现Eqv的结果与Xor操作的相反. 这意味着您还可以通过倒置Xor:

But if you take a close look to the truth table you might notice that the results for Eqv are exactly the opposite of an Xor operation. Meaning that you can also achieve logical equivalence operation with an inverted Xor:

If (!(Expression1 -xor $Expression2)) {...}

对于Xor的反转内容(整个操作或表达式之一)都无关紧要,因此您甚至可以将其简化为:

And as it doesn’t matter what you invert for an Xor (either the whole operation or one of the expressions), you can even simplify it to:

If (!Expression1 -xor $Expression2) {...}


检查

0..3 | ForEach {
    $Expression1, $Expression2 = [Int]($_ / 2), [Int]($_ % 2)
    New-Object PSObject -Property @{
        Expression1 = [Bool]$Expression1
        Expression2 = [Bool]$Expression2
        Equivalence = !$Expression1 -xor $Expression2
    }
} | Format-Table -AutoSize

真相表

Expression1 Expression2 Equivalence
----------- ----------- -----------
      False       False        True
      False        True       False
       True       False       False
       True        True        True

注意:在此解决方案中,将$Null表达式视为$False.这与VBScript Eqv实现不同,但与包含$Null表达式的其他PowerShell运算符一致.例如VBScript语句:If 1 And vbNull Then msgbox "True" Else msgbox "False",返回True,而PowerShell语句If (1 -and $Null) {"True"} Else {"False"},返回False.

Note: In this solution $Null expressions are considered $False. This differs from the VBScript Eqv implementation but is consistent with other PowerShell operators that contain $Null expressions. e.g. The VBScript statement: If 1 And vbNull Then msgbox "True" Else msgbox "False", returns True where the PowerShell statement If (1 -and $Null) {"True"} Else {"False"}, returns False.

如果您要查找按位的Eqv运算符(如果存在,应该将其称为-bEqv),那么它将是:

If you looking for a bitwise Eqv operator (which should probably be called -bEqv, if it existed), then it would be:

$Equivalence = -bNot Expression1 -bXOr Expression2    # e.g.: -bNot 3 -bXOr 5 = -7 (-bAnd 0xF = 9)

这篇关于转换VBScript的Eqv运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆