Perl – How to check if a key exists in a hash in Perl?


I want to check if parameter $PGkey is equal to a key with the same name inside a hash table. Further, I want to do it in a format as close to this as possible:

while(<PARAdef>) {
    my($PGkey, $PGval) = split /\s+=\s+/;
    if($PGkey == $hash{$PGkey}) {
        print PARAnew "$PGkey = $hash{$PGkey}->[$id]\n";
    } else {
        print PARAnew "$PGkey = $PGval\n";

Is there a simple way to do it?

Best Solution

The way to check for hash key existence is:

exists $hash{$key}