$HASH_LIMIT = 1024; ## myhash (key:string) : int # # calculate a checksum # sub myhash { my $key = shift; my $h = 0; for $c (unpack ('c*', $key)) { ## break the key into chars $h *= 2; ## shift the hash value left one bit $h ^= $c; ## and XOR it with the next char } $h %= $HASH_LIMIT; ## trim to fit the defined range return ($h); ## and return the result } for $k (qw( testing the hash function )) { printf ("%10s - %s\n", $k, &myhash($k)); } #### @MY_HASH = (0) x $HASH_LIMIT; ## put (key:string, val:string) : nil # # store a value in a hash # sub put { my $key = shift; my $val = shift; $MY_HASH[ &myhash($key) ] = $val; return; } ## get (key:string) : string # # get a value from the hash # sub get { my $key = shift; return ($MY_HASH[ &myhash($key) ]); }