Hash string


 
Thread Tools Search this Thread
Top Forums Programming Hash string
# 1  
Old 06-02-2012
Hash string

Hello everyone!

I'm writing a C++ program and I use a hash table which contains strings.
Its the first time I have to deal with strings since the only thing I have done so far is for integers and is the following:

Code:
int hashFunction(int key){ 
	return key%sizeOfHashTable;
}

but how can I do the same for strings?
I need the result to give an integer as the hashing is based on integers although the hash table elements are strings.Any ideas?

To be more specific I still want my function to return an integer although it takes a string as an argument(key)

Last edited by vlm; 06-02-2012 at 07:12 AM..
# 2  
Old 06-04-2012
It depends on how you want to use the int output of the hash function...is it to be used as an index into the array...in which case you can add up the ascii values of each of the individual characters that make up the string modulo the no of elements of the array in order to get the index into the array.
This User Gave Thanks to shamrock For This Post:
# 3  
Old 06-04-2012
As an aside, that's not a particularly good method for hashing an integer. It means integers n, n+1, n+2, etc will all end up in a row in the table, which will make searching for empty slots slow if there's a collision. More often used is the 'fibbonaci hash', which works by multiplying the key by a 'relatively prime' number before taking the modulous. This spreads out the resulting array indexes across the table.

Code:
int hash(int value) { return((value*2654435769)%tablesize);

There are many, many different algorithms for hashing strings, some good, some bad. The idea is to take the characters one at a time and combine them in some way into a single integer. A fairly bad method is to just sum up the values of all the characters and then take the modulous of that. Bad because, "aaA", "aAa", "Aaa" would all have the same hash because they have the same sum.

Better methods mangle the character in various ways depending on its position in the string. This one shifts it one bit for each position, wrapping at 23 bits, to give better odds that "aaA", "aAa", "Aaa" don't hash to the same string. I make no guarantees that it's ideal in any way but is at least better than summing.

Code:
int hash(const char *val)
{
        unsigned int n=0, hash=0;
        while(val[n])
        {
                hash ^= ((unsigned int)val[n]) << (n%23)
                n++;
        }

        return(hash%tablesize);
}

There'll always be a chance at hash collisions because there's more than 2^32 different strings in the world. But you do what you can to reduce the odds.

Last edited by Corona688; 06-04-2012 at 01:48 PM.. Reason: whoops, bug
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to print hash of hash in table format

Hi, I have a hash of hash where it has name, activities and count i have data like this - $result->{$name}->{$activities} = $value; content of that are - name - robert tom cat peter activities - running, eating, sleeping , drinking, work i need to print output as below ... (3 Replies)
Discussion started by: asak
3 Replies

2. Shell Programming and Scripting

Sort a hash based on the string length of the values

Hi, I want to be able to sort/print a hash based on the string length of the values. For example %hash = ( key1 => 'jeri', key2 => 'corona', key3 => 'una, ); I want to be able to print in the following order (smallest to largest) una,jeri,corona OR... (1 Reply)
Discussion started by: jdilts
1 Replies

3. Shell Programming and Scripting

Dynamically parse BibTeX and create hash of hash

Hello gurus, Iam trying to parse following BibTex file (bibliography.bib): @book{Lee2000a, abstract = {Abstract goes here}, author = {Lee, Wenke and Stolfo, Salvatore J}, title = {{Data mining approaches for intrusion detection}}, year = {2000} } @article{Forrest1996, abstract =... (0 Replies)
Discussion started by: wakatana
0 Replies

4. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

5. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

6. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

7. Shell Programming and Scripting

Assigning a hash to another hash key

Hello, I have a hash in hsh. I need to assign it to another hash globalHsh. I think the below statement does not work $globalHsh{$id} = %hsh; What is the right way to assign it? Thanks (3 Replies)
Discussion started by: rsanjay
3 Replies

8. Shell Programming and Scripting

Print Entire hash list (hash of hashes)

I have a script with dynamic hash of hashes , and I want to print the entire hash (with all other hashes). Itried to do it recursively by checking if the current key is a hash and if yes call the current function again with refference to the sub hash. Most of the printing seems to be OK but in... (1 Reply)
Discussion started by: Alalush
1 Replies

9. Shell Programming and Scripting

String hash/obfuscation in ksh

I have a vendor that needs to install a set of scripts (written in korn) that will be run as root through crontab every day. This set of scripts will need to ssh as root to other servers without getting challenged for user name or password. So I have set up ssh key pairing and authorized_keys... (2 Replies)
Discussion started by: StHalcyon
2 Replies

10. Programming

md5 hash a string or char array in SCO

Can someone provide me with code to md5 hash a string or character array in the SCO environment. All help is appreciated thanks. (5 Replies)
Discussion started by: jcarter2333
5 Replies
Login or Register to Ask a Question