Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The code below is pretty simple but I can't figure out why it is not printing the hash key? I'm sure someone here can figure it out in a matter of seconds where I have been looking at it for over an hour.
use strict; use warnings; my %hash = ( 'asdf.com' => 'asdf', 'abc.123.com' => '123', 'just.me.com' => 'me', 'just.you.com' => 'you', 'new.domain.com' => 'new', 'newest.domain.com' => 'newest' ); print "\nPossibly domain names: \n\n"; foreach my $key (keys %hash) { print " $key\n"; } print "\nWhat is the domain name of this server? \n"; my $selection = <>; chomp $selection; foreach my $key (keys %hash) { if ($key eq $selection) { print "\n"; }else{ print "\nThe domain you entered is not on the list above. Now + exiting script.\n"; exit; } }
Thank you in advance.

Replies are listed 'Best First'.
Re: Why is this code not printing hash key?
by Athanasius (Archbishop) on Apr 29, 2014 at 13:49 UTC

    Umm... because in these lines:

    if ($key eq $selection) { print "\n";

    you’re just printing a newline, but not the key?

    BTW, instead of the foreach loop I would say:

    if (exists $hash{$selection}) { print "\n$selection\n"; } else { print "\nThe domain you entered is not on the list above. Now exi +ting script.\n"; exit; }

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Why is this code not printing hash key?
by NetWallah (Canon) on Apr 29, 2014 at 13:54 UTC
    You are exiting the 'for' loop at the first key that mismatches.

    i.e. you are not checking ALL keys.

    You are also not using the hash at what it is best at - direct lookups of the key.

    Anyway - your code would work better if coded as:

    my $selection = <>; chomp $selection; my $found=0; foreach my $key (keys %hash) { if ($key eq $selection) { print "\n"; $found=1; last; } } $found or print "\nThe domain you entered is not on the list above. + Now exiting script.\n";

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

Re: Why is this code not printing hash key?
by bigj (Monk) on Apr 29, 2014 at 13:52 UTC
    Your logic for finding out whether the domain is in the hash is wrong. You should use something like
    if (exists $hash{$key}) { print "\n"; } else { print "\nThe domain you entered is not on the list above ..."; }
    You loop through the keys of your hash, so you take the first key (attention: they are usually not in the same order as you put them in the hash) and compares it to your $selection. Most likely it won't be the same, so your else-part is executed, where you have built in an exit, too, so it prints your failure statement and stops. Even if your $selection is one of your keys, unless it is the first key in the hash, your program won't find it.

    Greetings,
    Janek Schleicher

    PS: In general it is not a good idea to name a hash %hash. That it is a hash is shown by the sigil %. Better give it a name that describes what's inside the hash, here an idea would be to call that variable like %domain. Something similiar could be said about $key, but here you could avoid it completely, e.g. to print the list of domains, you could just write print "  $_\n" foreach keys %hash