Created
June 1, 2013 13:18
-
-
Save vaiorabbit/5690344 to your computer and use it in GitHub Desktop.
FNV-1a Hash (http://isthe.com/chongo/tech/comp/fnv/) in Perl.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # $ 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