Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Hash question

by spencerr1 (Novice)
on Apr 13, 2013 at 16:11 UTC ( #1028530=perlquestion: print w/ replies, xml ) Need Help??
spencerr1 has asked for the wisdom of the Perl Monks concerning the following question:

I'm doing an exercise. I have an hash. Course number => Course name

%course (

"2CPR2B" => "C Language",

"4PL400" => "Perl Language");

I'm asking for user to enter course number on cmd line and I want to print the value of the entered course number. For example

You are taking (course name) this summer

thanks Mark

Comment on Hash question
Re: Hash question
by NetWallah (Abbot) on Apr 13, 2013 at 16:39 UTC
    Your code as posted does not compile.

    The correct syntax would be:

    my %course =( "2CPR2B" => "C Language", "4PL400" => "Perl Language");
    Now, to access the %course hash, you need to index it by a key. Hashes require the use of braces "{}" for indexing (aka subscripting). for example,
    print $course{"2CPR2B"}, "\n";
    Would print out "C Language".

    See List value constructors for more information on how to populate hashes, and extract from them.

    The "subscripts" section of "perldoc perldata" has information on the syntax of hash subscripts.

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

      Thanks and thanks for the heads up on the perl doc

Re: Hash question (perlintro/perlfaq)
by Anonymous Monk on Apr 14, 2013 at 08:41 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1028530]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-08-01 01:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls