36600

Converting VBScript's Imp Operator

Question:

If you google for <a href="https://www.google.nl/search?q=PowerShell%20Imp" rel="nofollow">PowerShell Imp</a> you currently will find the <a href="https://technet.microsoft.com/library/ee176871.aspx" rel="nofollow">VBScript-to-Windows PowerShell Conversion Guide</a> on top of the list. Yet the answer is much of technical help:

<em>Imp Operator</em>

<strong><em>Definition:</em></strong> Performs a logical implication on two expressions.

<em>What do we know about the Imp operator? Two things: 1) it’s not supported in Visual Basic .NET, and 2) we have never heard of anyone actually using it. Therefore, we’re going to say that it doesn’t really matter whether or not there’s a Windows PowerShell equivalent.</em>

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

Example 1

I want to copy any file that is not hidden except if it is set to be archived (it also needs to be copied).<br /> A command for this would have been something like:

If (($File.Attributes -match "Hidden") -imp ($File.Attributes -match "Archive")) {Copy $File}

Example 2

I have a Log-Entry cmdlet with a Log-Debug alias. Similar to the native Write-Host/Write-Debug functions; when the Log-Debug <em>alias</em> is used, the information not be revealed but only be recorded when the common -Debug is supplied. A command for this would have been something like:

If (($Noun -eq "Debug") -imp $MyInvocation.BoundParameters.Debug.IsPresent) {Add-content $LogFile $$Entry}

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

Answer1:

If you take the definition quiet literally, you will probably come to a statement like for a logical implication:

If ($Expression1) {If ($Expression2) {DoThis}} Else {DoThis}

Which might get quiet excessive as you need to execute twice the same function. So, it is preferable to have a single If condition as replacement for the VBScript's Imp operator which made me come up with:

If (($True, [Bool]$Expression2)[[Bool]$Expression1]) {...}

But a little further investigation led me even to a much simpler replacement for the VBScript's Imp operator:

<hr />If (!$Expression1 -or $Expression2) {...} <hr />

Check

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

Truth Table

Expression1 Expression2 Implication ----------- ----------- ----------- False False True False True True True False False True True True

<strong>Note:</strong> In this solution $Null expressions are considered $False. This differs from the VBScript Imp 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.

Bitwise

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

$Implication = -bNot Expression1 -bOr Expression2 # e.g.: -bNot 3 -bOr 5 = -3 (-bAnd 0xF = 13)

Recommend

  • Django mod-python error
  • VBA Filter Table and Copy Subset of Resulting Columns to Clipboard
  • SWIG ImportError: undefined symbol: _Py_RefTotal
  • Team Foundation Server 2015 vs. Bitbucket Server (Stash)
  • Does anyone have a Categorized XML Corpus Reader for NLTK?
  • CSS Grid, position absolute an element in a css grid item: IMPOSSIBLE
  • Load Same ACF Map Twice on Same Page
  • How to get real device model in Android?
  • Fat binaries in iOS
  • Quick Question About Get and Set
  • Get Currently Active User in Android
  • insert radio value multiple data codeigniter in database to one row?
  • how to get data attributes of dynamically generated element
  • Universal Image Loader reuse images
  • Most efficient way to move table rows from one table to another
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • ListItem.Attributes.Add not working
  • How to change the font size of a single index for UISegmentedControl?
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Functions in global context
  • Extracting HTML between tags
  • Get data from AJAX - How to
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Running a C# exe file
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Symfony2: How to get request parameter
  • Upload files with Ajax and Jquery
  • Google cloud sdk not working when python points python3
  • Why winpcap requires both .lib and .dll to run?
  • XCode can't find symbols for a specific iOS library/framework project
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • -fvisibility=hidden not passed by compiler for Debug builds
  • coudnt use logback because of log4j
  • Turn off referential integrity in Derby? is it possible?
  • Busy indicator not showing up in wpf window [duplicate]