Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by NetWallah (Abbot)
on Oct 10, 2012 at 06:12 UTC ( #998142=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re: trying to implement hash table. and getting lots of difficulties. plz help me out..........
Download Code
Re^2: trying to implement hash table. and getting lots of difficulties. plz help me out..........
by Priti24 (Novice) on Oct 10, 2012 at 07:34 UTC

    Thnks 4 ur reply. bt u didn't reply 4 my whole question. as i also asked if i search for 47 . then it is giving me index 7 . and at 7 index 37 is given . so its giving me 37 . bt it should give me that this key is not available. means how colud i compare my whole keys with key 47. so i can say it is not avaialbe

    and one thing more if i searched for 201 then there are two keys there then how it will give 201.

      #!/usr/local/bin/perl use warnings; use strict; use Text::Autoformat 'autoformat'; my $horrid = <<'_INPUT_'; Thnks 4 ur reply. bt u didn't reply 4 my whole question. as i also ask +ed if i search for 47 . then it is giving me index 7 . and at 7 index 37 is gi +ven . so its giving me 37 . bt it should give me that this key is not available +. means how colud i compare my whole keys with key 47. so i can say it is not avaialbe and one thing more if i searched for 201 then there are two keys there then how it will give 201. _INPUT_ for ( $horrid ) { s/\b i \b/I/gxms; s/\b bt \b/but/gxms; s/\b 4 \b/for/gxms; s/\b u \b/you/gxms; s/\b ur \b/your/gxms; s/[ ]([.?!])/$1/gms; } print autoformat( $horrid , { 'squeeze' => 1 , 'justify' => 'left' , 'case' => 'sentence' , 'left' => 0 , 'right' => 78 } ); __END__ Thnks for your reply. But you didn't reply for my whole question. As I + also asked if I search for 47. then it is giving me index 7. and at 7 index + 37 is given. So its giving me 37. but it should give me that this key is not available. Means how colud I compare my whole keys with key 47. so I c +an say it is not avaialbe and one thing more if I searched for 201 then there are two keys there then how it will give 201.
      If you want to fail the search when the item is not found, you need to add the following line before the final "print"
      die "$k was not found (slot $h)" unless grep {$k==$_} keys (%{$buckets +[$h][0]});
      There are actually 3 keys in the "201" slot.
      If you run the code I posted, it will show all three.
      If you wanted to show only the matched key, you can use a grep statement , very similar to the one above.

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

        thnk u very much.....

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (15)
As of 2014-10-30 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls