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:
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)
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.
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.
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.
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
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)
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)
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)
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)
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)
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)
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)
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)