perl hash issue

Hi All,

#!/usr/local/bin/perl
my %hash = (
        en => {
                name => "english"
        },
        de => {
                name => "german"
        }
        );


my $lang = "kk";
my @keys_arr = keys %hash;
print "Before the exists condition check the keys are @keys_arr\n";

if( exists $hash{$lang}{name}){
        print "THE Language name is $hash{$lang}{name}\n";
}else{
        print "THE Language is not found \n";
}

@keys_arr = keys %hash;
print "After the exists condition check the keys are @keys_arr\n";

Now when i print the array , why does the value kk also occur in the array??

Please let me know if i am missing anything here.
Regards,
Lakshmi
lakshminairAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kanduraCommented:
That's because when you're saying $hash{kk}{name}, it auto-vivifies $hash{kk} with an empty hashref.
Try writing your if like this:

    if( exists $hash{$lang} ) {
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITcrowCommented:

if( $hash{$lang}  && $hash{$lang}{name} ) {
        print "THE Language name is $hash{$lang}{name}\n";
}else{
        print "THE Language is not found \n";
}
0
rugdogCommented:
or

if( defined $hash{$lang}{name}){
       print "THE Language name is $hash{$lang}{name}\n";
}else{
       print "THE Language is not found \n";
}
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

kanduraCommented:
> if( defined $hash{$lang}{name}){

that also autovivifies $hash{kk}.
0
rugdogCommented:
you're right, I missed the my $lang='kk' while testing it and thought it worked
0
godspropyCommented:
To get perl not to autovivify the hash you can access the $hash{$lang} as a pointer. This worked in my testing:

if( exists $$hash{$lang}{name}){
0
godspropyCommented:
Sorry about that, the above post actually does not find existing values either. However, ITcrow has the proper solution above.
0
leitzlerCommented:
You even might have to check them one at a time:

if( exists $hash{$lang} ) {
     if ( exists $hash{$lang}{'name'} ) {
          print "THE Language name is $hash{$lang}{'name'}\n";
     }
}
0
ozoCommented:
that is essentialy equivalent to ITcrow's solution

print "THE Language name is $hash{$lang}{name}\n" if exists $hash{$lang} && exists $hash{$lang}{name};
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.