Perl validation on hash key is defined or not

rajeshkumar created the topic: perl validation on hash key is defined or not

Solutions Thanks Len for example program.

our %SERVERS = (
“rajesh1” => {type => ‘prod’, sha => ‘ram’},
“rajesh2” => {type => ‘prod’, sha => ‘sita’},
“rajesh3” => {type => ‘xyz’, named => [“raa”]},
“rajesh4” => {type => ‘xxx’, named => [“rajjaj”]},
);

while (my $mServer = each %SERVERS) {
if ($SERVERS{$mServer}{type} eq “prod” && !$SERVERS{$mServer}{named}) {
print “$mServer\n”;
}
}

Other sols

if ($SERVERS{$mServer}->{type} eq “prod” &&
! defined $SERVERS{$mServer}->{named} )

More Reference:
perldoc.perl.org/functions/exists.html

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Rajesh Kumar
Total Page Visits: 93 - Today Page Visits: 1