Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

Replies are listed 'Best First'.
Re: Hash question
by NetWallah (Canon) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1028530]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2018-06-19 10:46 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.