简体   繁体   中英

Is -and comparison evaluated individually or against the same object?

I am looking for missing events between two arrays: $tc_records & $RelationshipEvents_array . Both arrays should have the same two entries.

An entry for A->B and reverse B->A. When I'm evaluating RelationshipEvents with -and statement, does one (same) event gets evaluated, or each side of the -and loads the array and evaluates is independently.

foreach ($record in $tc_records) {
    # is this using the same RelationshipEvent for both comparisons or two different comparisons?
    if($RelationshipEvents_array.entity1Id -eq $record.entity1Id -and $RelationshipEvents_array.entity2Id -eq $record.entity2Id){
        $re_tc_matched_record.Add($record)
    } else {
        $re_tc_not_matched_record.Add($record)
    }    
}
in case this makes any difference:
Name                           Value
----                           -----
PSVersion                      6.2.3
PSEdition                      Core
GitCommitId                    6.2.3
OS                             Darwin 19.0.0 Darwin Kernel Version 19.0.0: Thu Oct 17 16:17:15 PDT 2019; root:xnu-6153.41.3~29/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

It looks like your two arrays hold similar objects with various properties. When you perform this:

foreach ($record in $tc_records) {
    if($RelationshipEvents_array.entity1Id -eq $record.entity1Id -and $RelationshipEvents_array.entity2Id -eq $record.entity2Id)
...

You're actually comparing an array holding all the entity1Id properties (calculated by getting the property of that name from each of the objects in the $RelationshipEvents_array array) with the entity1Id property of the current $record object.

This is unlikely to ever evaluate to true given the different object types unless I've misunderstood your question.

What I would normally do when making a comparison like this is something like the following to get a match:

if($RelationshipEvents_array.entity1Id -contains $record.entity1Id)
...

However, since you're trying to match two properties the simplest way might be to:

Iterate through the array for each record you check in a second foreach loop

foreach ($record in $tc_records) {
    foreach ($event in $RelationshipEvents_array) {
        if ($record.entity1Id -eq $event.entity1Id -and $record.entity2Id -eq $event.entity2Id)
{ # Do matchy stuff
...

You do have the compare object cmdlet to do array comparisons for you with quite a few options to output non-matching objects between the arrays.

$a1=@(1,2,3,4,5)
$b1=@(1,2,3,4,5,6)

Compare-Object -ReferenceObject $a1 -DifferenceObject $b1

To answer your question though, -and is a just evaluating a separate 2nd function that must also equal true for the statement to be evaluated.

if (true){#runs}
if (false){#no runs}
if (true -and true){#runs}
if (true -and false){#no runs}
if (false -and true){#no runs}

You seem to be using -eq on a separate array, hard to tell exactly with no example data but you should be able to test your logic by simply doing $RelationshipEvents_array.entity1Id -eq $record.entity1Id and checking if it returns true or false as you are expecting it would. If $RelationshipEvents_array is an array you probably want to use -contains

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM