Priti24 has asked for the wisdom of the Perl Monks concerning the following question:

i m new to perl. and i am trying to implement hash table . firstly i took a hash like

%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" );

i also took an array with 10 ten empty buckets like

@buckets = ( [],[],[],[],[],[],[],[],[],[] );

after that i used a while loop and got key from there and after that i called a function perlhash to calculate the index of that key. after getting index of every particular key in hash , i pushed every key , value pair at index which i got from from perlhash function. if two keys having same index then it will store two keys at same index

sub perlhash { $hash = 0; $hash = ($k % 10); return $hash; } while(($k , $v) = each(%ex)) { $hash = perlhash($k); $ch = $buckets[$hash]; print Dumper $buckets[$hash]; if(scalar @{$ch} == 0){ $entry = {$k => $v}; }else{ $entry = $ch->[0]; $entry->{$k} = $v ; } push @$ch , $entry; } print Dumper @buckets;

now i want to search for a particular key existence and i want to get value for that key. how should i do that with my code.

print "enter a key u want to search"; $k = <STDIN>; $h = perlhash($k);

suppose i want to search 37. then this $h giving me index of 37. and through $buckets[$h] i can get value like.

my $VAR1 = [ { '37' => 't', } ];

how could i get 37 from this code. also if i search for 47 then this will also gave me same index then i have to apply a comparison that at this index 47 does not exist. and also how could i search 101 and its value here

firstly execute this code then u will understand in a better way

plz help me out from this problem

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

Replies are listed 'Best First'.
Re: trying to implement hash table. and getting lots of difficulties. plz help me out..........
by kcott (Archbishop) on Oct 10, 2012 at 07:25 UTC

    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

Re: trying to implement hash table. and getting lots of difficulties. plz help me out..........
by NetWallah (Canon) on Oct 10, 2012 at 06:12 UTC
    Your structure and logic is much more complicated than necessary, but I have preserved it, and made a few corrections. Try this one:
    use strict; use warnings; use Data::Dumper; 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" ); my @buckets = ( [],[],[],[],[],[],[],[],[],[] ); sub perlhash { my $k = shift @_; my $hash = 0; $hash = ($k % 10); return $hash; } while(my ($k , $v) = each(%ex)) { my $hash = perlhash($k); my $ch = $buckets[$hash]; my $entry; # print Dumper $buckets[$hash]; if(scalar @{$ch} == 0){ $entry = {$k => $v}; }else{ $entry = $ch->[0]; $entry->{$k} = $v ; next; } push @$ch , $entry; } print Dumper \@buckets; print "enter a key u want to search"; chomp(my $k = <STDIN>); my $h = perlhash($k); print "The Key in Slot $h is " . join (",",keys (%{$buckets[$h][0]}) +) . "\n" ;

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

    A reply falls below the community's threshold of quality. You may see it by logging in.