G'day Priti24,
Perl already provides the functionality you describe. So, unless you have a specific reason for re-inventing the wheel (which may be perfectly valid but isn't addressed in your post), you can use code like this to achieve what you're after.
#!/usr/bin/env perl
use strict;
use warnings;
my %ex = (
37 => 'p', 41 => 't', 23 => 's', 52 => 'm', 44 => 'a', 65 => 'q',
26 => 'd', 88 => 'o', 99 => 'g', 100 => 'l', 101 => 'f', 201 => 'i
+'
);
print 'Enter the key you want to search for: ';
my $k = <>;
chomp $k;
if (exists $ex{$k}) {
print "The key '$k' has the value: $ex{$k}\n";
}
else {
print "The key '$k' doesn't exist.\n";
}
Here it is in action with the three keys you mention (37, 47 & 101):
$ pm_hash_search.pl
Enter the key you want to search for: 37
The key '37' has the value: p
$ pm_hash_search.pl
Enter the key you want to search for: 47
The key '47' doesn't exist.
$ pm_hash_search.pl
Enter the key you want to search for: 101
The key '101' has the value: f
Here's some of the things you could have done better in the code you provided:
-
Let Perl tell you when you're doing something potentially problematic, or just plain wrong, with strict and warnings. See usage in my code.
-
Declare your variables. my is used most often - see my %ex and my $k in my code. With no declarations, all your variables become global which has all sorts of implications and is basically a headache you don't need to have. (There are other ways to declare variables - see perlsub for details.)
-
You call your subroutine with an argument (perlhash($k)) but don't read that argument in the subroutine code. In this instance, my $k = shift; would have been sufficient; for multiple arguments, use something like my ($arg1, ..., $argN) = @_; - see perlsub for details.
-
It's easy to mistype, misread or miscount the exact number of arrayrefs in @buckets = ( [],[],[],[],[],[],[],[],[],[] );. You can use the x operator to avoid all three types of problems: @buckets = ([]) x 10; - see perlop for details.
-
When you read user input (typed from the keyboard) you'll get all the keystrokes including the final return. Use chomp to remove this (as I did in my code).
-
Your code would be easier to read if it was laid out a little better - perlstyle has some suggestions regarding this.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.