Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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 (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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1028530]
Approved by Old_Gray_Bear
[Mj1234]: Hello Monks, I have installed Active perl 5.16 and 5.14 but I dont find Devel::GDB package when I try to install through ppm
[Discipulus]: i'v installed ubuntu 16.04lts and i'm quite disappointed: is deadly simmilar to new win osses where you cannot find even simple things...
[Corion]: Hi Discipulus ;)
[Corion]: Mj1234: Maybe Ubuntu didn't package Devel::GDB? In that case, consider using local::lib and install the module from CPAN
[Corion]: But a quick Google search turns up this page, which seems to suggest that it should be packaged
[Discipulus]: iirc active perl has repositories only for last 2 o 3 major release of perl Mj1234. I strongly suggest portable version of strawberry perl and use cpan client to install modules
[choroba]: Also, who needs GDB when there's hdb?

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2016-12-07 08:55 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (125 votes). Check out past polls.