views:

38

answers:

1

Is there a way to grab the key of one map and replace the value of the other with its value?

def wild = [animal1:"pet3", animal2:"dog", animal3:"pig"]
def pet = [pet1:"hamster", pet2:"fish", pet3:"cat"]

if(pet.containsKey(wild.animal1)) {
    //replace wild.animal1 with the value contained in pet3 for example
    //so wild.animal1 would equal "cat"
} else {
    //dont change value
}

So basically I'm wondering if I am able to find a key based on a value in the map wild and replacing the value with the value of the key in the map pet.

Is there a simple way of going about this?

+1  A: 
if(pet.containsKey(wild.animal1))
{
    wild.animal1 = pet[wild.animal1];
}
Matthew Flaschen
Wasn't aware one could assign it directly like that. Lol feel a bit dumb that it was so simple. Thank you for the help Matthew
StartingGroovy