Skip to content

Instantly share code, notes, and snippets.

@vaiorabbit
Created June 1, 2013 13:18
Show Gist options
  • Select an option

  • Save vaiorabbit/5690344 to your computer and use it in GitHub Desktop.

Select an option

Save vaiorabbit/5690344 to your computer and use it in GitHub Desktop.
# $ cat words.txt | perl Hashcode.pl words.txt > result_perl.txt
use strict;
main() unless caller;
sub fnv32a {
my $str = $_[0];
my $FNV1_32_PRIME = 0x01000193;
my $UINT32_MAX = 4294967296; # 2 ** 32
my $hval = 0x811c9dc5; # FNV1_32A_INIT
my $s = 0;
foreach $s (split //, $str) {
$hval = $hval ^ ord($s);
$hval = ($hval * $FNV1_32_PRIME) % $UINT32_MAX
}
return $hval;
}
sub main
{
open F, $ARGV[0] or die $!;
while (<F>) {
$_ =~ /\"(.*)\",/;
my $word = $1;
print $word, " -> ", fnv32a($word), "\n";
}
close F;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment