Created
October 7, 2024 11:14
-
-
Save jgrahamc/ae5b030be5cdffb424d1ae126023190f to your computer and use it in GitHub Desktop.
Calculate the check digit in a Portuguese NIF
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
use strict; | |
use warnings; | |
sub nif_check { | |
my ($nif) = @_; | |
my $check = 0; | |
foreach my $i (reverse 0..7) { | |
$check += substr($nif, $i, 1) * (9-$i); | |
} | |
$check %= 11; | |
if ($check < 2) { | |
$check = 0; | |
} else { | |
$check = 11 - $check; | |
} | |
print "$nif $check\n"; | |
} | |
nif_check($ARGV[0]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment