in reply to ascii to binary
use List::Util qw( sum ); my $id = (sum map ord, map /./sg, $word) % 65536;
Ref: ord, List::Util
Update: The above will underflow on long strings and overflow on longer strings. Fix:
my $id = 0; foreach (map ord, map /./sg, $word) { $id = ($id + $_) % 65536; }
Update: What you are doing is called hashing. The following uses a better hashing function. Of course, that means it'll return a different number than yours.
use Digest::MD5 qw( md5 ); my $id = unpack('n', substr(md5($word), -2));
Ref: Digest::MD5, unpack
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: ascii to binary
by Marsel (Sexton) on Dec 04, 2006 at 16:39 UTC | |
by GrandFather (Saint) on Dec 04, 2006 at 19:01 UTC |