Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: trying to implement hash table. and getting lots of difficulties. plz help me out..........

by kcott (Abbot)
on Oct 10, 2012 at 07:25 UTC ( #998163=note: print w/ replies, xml ) Need Help??


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:

  • 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.

-- Ken


Comment on Re: trying to implement hash table. and getting lots of difficulties. plz help me out..........
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998163]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-09-18 12:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (113 votes), past polls