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