Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

perl hash issue

Posted on 2004-10-28
11
Medium Priority
?
260 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
Comment
Question by:lakshminair
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
11 Comments
 
LVL 18

Accepted Solution

by:
kandura earned 200 total points
ID: 12433285
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
ITcrow earned 200 total points
ID: 12439759

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
ID: 12444641
or

if( defined $hash{$lang}{name}){
       print "THE Language name is $hash{$lang}{name}\n";
}else{
       print "THE Language is not found \n";
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 18

Expert Comment

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

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

Expert Comment

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

Expert Comment

by:godspropy
ID: 12545968
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
ID: 12546232
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
ID: 12653768
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
ID: 12660052
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…
Six Sigma Control Plans

636 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