<?xml version="1.0" encoding="windows-1252"?>
<node id="998163" title="Re: trying to implement hash table. and getting lots of difficulties. plz help me out.........." created="2012-10-10 03:25:46" updated="2012-10-10 03:25:46">
<type id="11">
note</type>
<author id="861371">
kcott</author>
<data>
<field name="doctext">
&lt;p&gt;G'day [Priti24],&lt;/p&gt;
&lt;p&gt;
Perl already provides the functionality you describe. So, unless you have a specific reason for &lt;em&gt;re-inventing the wheel&lt;/em&gt; (which may be perfectly valid but isn't addressed in your post), you can use code like this to achieve what you're after.
&lt;/p&gt;
&lt;code&gt;
#!/usr/bin/env perl

use strict;
use warnings;

my %ex = (
    37 =&gt; 'p', 41 =&gt; 't', 23 =&gt; 's', 52 =&gt; 'm', 44 =&gt; 'a', 65 =&gt; 'q',
    26 =&gt; 'd', 88 =&gt; 'o', 99 =&gt; 'g', 100 =&gt; 'l', 101 =&gt; 'f', 201 =&gt; 'i'
); 

print 'Enter the key you want to search for: ';
my $k = &lt;&gt;;
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";
}
&lt;/code&gt;
&lt;p&gt;
Here it is in action with the three keys you mention (37, 47 &amp;amp; 101):
&lt;/p&gt;
&lt;code&gt;
$ 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
&lt;/code&gt;

&lt;p&gt;
Here's some of the things you could have done better in the code you provided:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Let Perl tell you when you're doing something potentially problematic, or just plain wrong, with [http://perldoc.perl.org/strict.html|strict] and [http://perldoc.perl.org/warnings.html|warnings]. See usage in my code.
&lt;/li&gt;
&lt;li&gt;
Declare your variables. [http://perldoc.perl.org/functions/my.html|my] is used most often - see &lt;c&gt;my %ex&lt;/c&gt; and &lt;c&gt;my $k&lt;/c&gt; 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 [http://perldoc.perl.org/perlsub.html|perlsub] for details.)
&lt;/li&gt;
&lt;li&gt;
You call your subroutine with an argument (&lt;c&gt;perlhash($k)&lt;/c&gt;) but don't read that argument in the subroutine code. In this instance, &lt;c&gt;my $k = shift;&lt;/c&gt; would have been sufficient; for multiple arguments, use something like &lt;c&gt;my ($arg1, ..., $argN) = @_;&lt;/c&gt; - see [http://perldoc.perl.org/perlsub.html|perlsub] for details.
&lt;/li&gt;
&lt;li&gt;
It's easy to mistype, misread or miscount the exact number of arrayrefs in &lt;c&gt;@buckets = ( [],[],[],[],[],[],[],[],[],[] );&lt;/c&gt;. You can use the &lt;c&gt;x&lt;/c&gt; operator to avoid all three types of problems: &lt;c&gt;@buckets = ([]) x 10;&lt;/c&gt; - see [http://perldoc.perl.org/perlop.html|perlop] for details.
&lt;/li&gt;
&lt;li&gt;
When you read user input (typed from the keyboard) you'll get all the keystrokes including the final return. Use [http://perldoc.perl.org/functions/chomp.html|chomp] to remove this (as I did in my code).
&lt;/li&gt;
&lt;li&gt;
Your code would be easier to read if it was laid out a little better - [http://perldoc.perl.org/perlstyle.html|perlstyle] has some suggestions regarding this.
&lt;/li&gt;
&lt;/ul&gt;


&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-861371"&gt;
&lt;p&gt;-- Ken&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
</field>
<field name="root_node">
998135</field>
<field name="parent_node">
998135</field>
</data>
</node>
