Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
snippets/damm/damm
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
62 lines (53 sloc)
1.36 KB
This file contains 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
#!/usr/bin/env perl | |
# damm.pl | |
# Author: William Woodruff | |
# ------------------------ | |
# A table implementation of the Damm check digit generation and validation | |
# algorithms. The Damm algorithm is similar to the Verhoeff algorithm, but | |
# detects all adjacent transposition errors instead of just most. | |
# ------------------------ | |
# This code is licensed by William Woodruff under the MIT License. | |
# http://opensource.org/licenses/MIT | |
use strict; | |
use warnings; | |
my @table = ( | |
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2], | |
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3], | |
[4, 2, 0, 6, 8, 7, 1, 3, 5, 9], | |
[1, 7, 5, 0, 9, 8, 3, 4, 2, 6], | |
[6, 1, 2, 3, 0, 4, 5, 9, 7, 8], | |
[3, 6, 7, 4, 2, 0, 9, 5, 8, 1], | |
[5, 8, 6, 9, 7, 2, 0, 1, 3, 4], | |
[8, 9, 4, 5, 3, 6, 2, 0, 1, 7], | |
[9, 4, 3, 8, 6, 1, 7, 2, 0, 5], | |
[2, 5, 8, 1, 4, 3, 6, 7, 9, 0]); | |
my ($command, $number) = @ARGV; | |
if (!$command || $command !~ /^(validate)|(generate)$/i | |
|| !$number || $number !~ /^\d+$/) { | |
usage(); | |
} | |
else { | |
damm(lc $command, $number); | |
} | |
sub usage { | |
print "Usage: $0 <validate | generate> <number>\n"; | |
exit; | |
} | |
sub damm { | |
my $cmd = shift; | |
my @n = split('', shift); | |
my $c = 0; | |
foreach (@n) { | |
$c = $table[$c][$_]; | |
} | |
if ($cmd eq "validate" && $c == 0) { | |
print @n, " is valid!\n"; | |
} | |
elsif ($cmd eq "generate") { | |
push @n, $c; | |
print @n, " is your number with a Damm check digit.\n"; | |
} | |
else { | |
print @n, " is not valid!\n"; | |
} | |
} |