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

I have a Perl Program. I am trying to create a hash and store the phone numbers in a hash. Write a program to prompt the user to enter a name from the hash. then print out the phone number of the person. My problem is i can prompt the user but I can't produce the phone number effectively, It's giving me all the numbers. Here is my program:
%friends= ("Bill" => "313-333-5555", "Bob" => "313-777-98755", "Joe" => "313-555-4435", ); print "Which phone number do you want?\n"; $friends= <STDIN>; print %friends;

Replies are listed 'Best First'.
Re: Perl Program w/ Hash
by kennethk (Abbot) on Apr 21, 2010 at 21:44 UTC
    You're getting confused between the key and the hash. The hash is a list of key value pairs, as you've saved. However, you access the values of %hash with the syntax $hash{$key}, so a working version of your code might look like:

    %friends= ("Bill" => "313-333-5555", "Bob" => "313-777-98755", "Joe" => "313-555-4435", ); print "Which phone number do you want?\n"; $name = <STDIN>; chomp $name; # To remove trailing newline print $friends{$name};
    To learn more about accessing hashes, see Perl variable types in perlintro.
Re: Perl Program w/ Hash
by ww (Archbishop) on Apr 21, 2010 at 21:52 UTC

    Please note: yours is the kind of question you need to be able to answer by your own efforts if you are trying to learn Perl.

    So, just possibly, you might start educating yourself by reading an (almost any) introduction to hashes with an emphasis on keys and values (and, for extra points, scope out the advice here in the Monastery on use strict; use warnings;). (Synopsis: Do so, without fail, unless you know precisely why not. Blatant self-promotion: Re: **HomeWork** Printing the found input from an array)

    For starters (and preliminary info; not a direct answer) try perldoc -f keys and perldoc -f values; then proceed directly to planetscape's excellent Not Exactly a Hash Tutorial.

    Alternately (or additionally) buy or borrow a copy of "Learning Perl" (Schwartz & Phoenix, O'Reilly); or, since this smells a bit like homework, if it is homework, try the textbook for your course.

Re: Perl Program w/ Hash
by superfrink (Curate) on Apr 21, 2010 at 21:43 UTC
Re: Perl Program w/ Hash
by choroba (Cardinal) on Apr 21, 2010 at 21:45 UTC
    Try to read perldata, especially the line 3 in the first example.
Re: Perl Program w/ Hash
by kiruthika.bkite (Scribe) on Apr 22, 2010 at 03:43 UTC
    Hash maintains the data as key-value pairs.
    The data which is left to the operator '=>' is known as key.
    And the data which is right to the operator '=>' is known as value.

    Using keys you can get the value of a particular key in a hash in the following way.
    $hash{'key'}

    And keys should be unique.

    In your case 'username' is a key and 'phone number' is a value.
    So using username you can get the phone number of the user.
    So,$friend{'username'} will give the phone number.
Re: Perl Program w/ Hash
by nvivek (Vicar) on Apr 22, 2010 at 03:52 UTC
    Refer this URL: Basic Information about hashes: http://www.tizag.com/perlT/perlhashes.php

    Hashes and References: http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/

    How hashes really work:
    http://www.perl.com/pub/a/2002/10/01/hashes.html