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


In reply to trying to implement hash table. and getting lots of difficulties. plz help me out.......... by Priti24

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.