in reply to trying to implement hash table. and getting lots of difficulties. plz help me out..........
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:
-- Ken
|
|---|