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
Follow me
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x