1

This might seem like a very stupid question, but I can't seem to figure it out. How can you put a relationship between values of an object?

I think the code will speak for itself:

$Collection = ("Fruit","Banana"),("Vegetables","Aubergine"),("Fruit","Appel"),("Vegetables","Courgette") | 
    ForEach-Object {
        [PSCustomObject]@{
            'Type'=$_[0]
            'Content'=$_[1]
        }
    }    
if ($Collection.Type -contains "Fruit" -and $Collection.Content -contains "Courgette") {
    Write-Host "We DID find a match" -ForegroundColor Green
}
else {
    Write-Host "We didn't find a match" -ForegroundColor Red
}

<# Result
Fruit + Banana = "We DID find a match"
Fruit + Courgette= "We DID find a match"
#>

How is it possible to only have a match when the pair matches? For example I would like it match in the case of Fruit + Banana but not in case of Fruit + Courgette.

I'm sorry if this sounds silly.. Thank you for your help.

1
  • 3
    $Collection | ?{$_.Type -eq "Fruit" -and $_.Content -eq "Banana"} ?? Commented Aug 12, 2014 at 10:09

2 Answers 2

2

You don't have to create a new custom object, PS already provides key/value collection in a shape of hash table. You can make this much simpler:

$h = @{ "Banana" = "Fruit"; "Aubergine" = "Vegetables"; "Appel" = "Fruit" ; "Courgette" = "Vegetables"}
function CheckObject($k, $v){
    if($h.Item($k) -eq $v){
        Write-Host "We DID find a match";
    }
    else{
        Write-Host "We did NOT find a match";
    }
}

Result:

PS U:\> CheckObject "Banana" "Fruit"
We DID find a match
PS U:\> CheckObject "Banana" "Vegetables"
We did NOT find a match
PS U:\> CheckObject "Courgetee" "Vegetables"
We did NOT find a match

The keys(first value) have to be unique, hence flipping from "Fruit","Banana".

Sign up to request clarification or add additional context in comments.

Comments

1

Your collection a unique value (Content) and a Type that can be repeated. In my solution I am assuming you are never going to have repeated entries like "Fruit/Banana" and "Vegetables/Banana"

Your logical test is: $Collection.Type -contains "Fruit" -and $Collection.Content -contains "Courgette"

What is the problem on your logical test? You are testing if your collection has "Fruit" (that is true) and if your collection has "Courguette" (that is also true).

You have to test if your collection has "Courgette" and if so, if the type of that "Courguette" object is "Fruit"...

You can create a function that looks for the $content and if it exists checks the type. It should return $true on a match.

function FindObject($type, $content)
    {
        $index = [array]::IndexOf($Collection.Content,$content)

        if (($index -ge 0) -and ($Collection[$index].Type -eq $type))
        {
            return $true
        }
        else
        {
            return $false
        }

    }

EDIT: As @Kayasax commented you can save code by using $Collection | ?{$_.Type -eq "Fruit" -and $_.Content -eq "Banana"} instead of a function. Less code lines but harder to read and debug if you want to understand.

Working example:

$Collection = ("Fruit","Banana"),("Vegetables","Aubergine"),("Fruit","Appel"),("Vegetables","Courgette") | 
    ForEach-Object {
        [PSCustomObject]@{
            'Type'=$_[0]
            'Content'=$_[1]
        }
    }    

function FindObject($type, $content)
{
    $index = [array]::IndexOf($Collection.Content,$content)

    if (($index -ge 0) -and ($Collection[$index].Type -eq $type))
    {
        return $true
    }
    else
    {
        return $false
    }

}

#if ( $Collection | ?{$_.Type -eq "Fruit" -and $_.Content -eq "Courgette"} )
if ( (FindObject "Fruit" "Courgette") -eq $true)
{
    Write-Host "We DID find a match" -ForegroundColor Green
}
else 
{
    Write-Host "We didn't find a match" -ForegroundColor Red
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.