我的工作场所不允许我们安装任何模块,因此该选项不适合我。因此决定查看此链接http://en.wikipedia.org/wiki/CUSIP并按照那里的伪代码并尝试在 Perl 中对其进行编码。
我想出了以下几点:
sub cusip_check_digit
{
my $cusip = shift; ## Input: an 8-character CUSIP
my $v = 0; ## numeric value of the digit c (below)
my $sum = 0;
for (my $i = 0; $i < 8; $i++)
{
my $c = substr ($cusip, $i, 1); ## $c is the ith character of cusip
if ($c =~ /\d/) ## c is a digit then
{
$v = $c; ## numeric value of the digit c
}
elsif ($c =~ /\w/)
{
my $p = ord($c) - 64; ## ordinal position of c in the alphabet (A=1, B=2...)
$v = $p + 9;
}
if (0 != $i % 2) ## check to see if $i is even (we invert due to Perl starting points)
{
$v = $v * 2;
}
$sum = $sum + int ($v / 10) + $v % 10;
}
$v = (10 - ($sum % 10)) % 10;
print "v is: $v\n";
#return (10 - ($sum % 10)) % 10
}
cusip_check_digit('90137F10'); ## should return 3 ** Now works **
cusip_check_digit('68243Q10'); ## should return 6 ** Now works **
不完全确定为什么它不起作用。