Solved

perl hash issue

Posted on 2004-10-28
258 Views
Last Modified: 2012-05-05
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
0
Question by:lakshminair
    9 Comments
     
    LVL 18

    Accepted Solution

    by:
    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
     
    LVL 5

    Assisted Solution

    by:ITcrow

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

    Expert Comment

    by:rugdog
    or

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

    Expert Comment

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

    that also autovivifies $hash{kk}.
    0
     
    LVL 7

    Expert Comment

    by:rugdog
    you're right, I missed the my $lang='kk' while testing it and thought it worked
    0
     
    LVL 2

    Expert Comment

    by:godspropy
    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
     
    LVL 2

    Expert Comment

    by:godspropy
    Sorry about that, the above post actually does not find existing values either. However, ITcrow has the proper solution above.
    0
     

    Expert Comment

    by:leitzler
    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
     
    LVL 84

    Expert Comment

    by:ozo
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now