By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

Problem with input and hashes

P: 22
Hello all,

I am in Perl programming class this term and am a beginner. I'm trying to be able to type any key of the hash i want using STDIN to pull the value of the key from the hash.

my program consists of this much so far.
Expand|Select|Wrap|Line Numbers
  1. %courses= (
  2.            '2CPR2B' => 'C Language',
  3.            '1UNX1B' => 'Intro to UNIX',
  4.            '3SH414' => 'Shell Programming',
  5.            '4PL400' => 'Perl Programming',
  6.           );
  7. foreach $value ( sort values(%courses)) {print "$value ";}
  8. print "\n";
  9.  
  10. # asking for input
  11. print "Please type the course number of the course you plan to take:";
  12. chomp($coursenum=<STDIN>);
  13. print "You will be taking %courses this semester. \n";

Everything before the last line works, i just have no idea on how to pull the exact value from the key which was input. I have been stuck on this problem for a few hours now and I can't seem to find any help from any website I have visited so far. Any help would be greatly appreciated. Thank you!
Apr 21 '09 #1
Share this Question
Share on Google+
4 Replies


eWish
Expert 100+
P: 971
You are not asking for the hash value in the proper manor. Here is an example.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %hash = (
  7.                 Perl     => 'Perl',
  8.                 VB         => 'Visual Basic',
  9.                 'C++'    => 'C Plus Plus',
  10.                 );
  11.  
  12. print "Please enter the class name\n";
  13.  
  14. my $param = <STDIN>;
  15.    chomp($param);
  16.  
  17. print "You will be taking a $hash{$param} class this semester\n";
  18.  
  19. 1;
This is what you needed $hash{$param}.

--Kevin
Apr 21 '09 #2

P: 22
Thank you for the reply Kevin. I really appreciate it. I changed a few things and i have added the use strict; and use warnings; to better help fix the problem. I modified my code to look like this:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. %courses= (
  4.            '2CPR2B' => 'C Language',
  5.            '1UNX1B' => 'Intro to UNIX',
  6.            '3SH414' => 'Shell Programming',
  7.            '4PL400' => 'Perl Programming',
  8.           );
  9. foreach $value ( sort values(%courses)) {print "$value ";}
  10. print "\n";
  11.  
  12. # asking for input
  13. print "Please type the course number of the course you plan to take:";
  14. $coursenum=<STDIN>;
  15. chomp($coursenum);
  16. print "You will be taking $courses{$coursenum} this semester. \n";
I am now recieving a list of errors stating:
Expand|Select|Wrap|Line Numbers
  1. Global symbol "%courses" requires explicit package name at elective line 3.
  2. Global symbol "$value" requires explicit package name at elective line 9.
  3. Global symbol "%courses" requires explicit package name at elective line 9.
  4. Global symbol "$value" requires explicit package name at elective line 9.
  5. Global symbol "$coursenum" requires explicit package name at elective line 14.
  6. Global symbol "$coursenum" requires explicit package name at elective line 15.
  7. Global symbol "%courses" requires explicit package name at elective line 16.
  8. Global symbol "$coursenum" requires explicit package name at elective line 16.
  9. Execution of elective aborted due to compilation errors.
I am not sure what exactly sure what these errors mean.
Apr 21 '09 #3

P: 22
Thanks Kevin! I found it works if i take off the strict and the warning. I don't know why i was getting those errors, maybe it was a local/global problem?
Apr 21 '09 #4

eWish
Expert 100+
P: 971
Have a look at the perl doc's. They are very insightful.

strict
warnings
scoping

Using the strict and warnings pragmas is important. Also, learn what scoping is about too. These will help detect errors.

--Kevin
Apr 21 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.