[oak perl] Re: Customizing a Barcode Checkdigit Subroutine
Belden Lyman
blyman at iii.com
Mon Dec 23 14:47:48 CST 2002
David B. Peterson wrote:
>
> There is an interesting property of numbers that you can use for this
> calculation.
>
> If you add the individual digits of a number and subtract 9 from it as
> many times as necessary, you will eventually get the sum of the two
> numbers.
For any base N counting system where N > 9, N - 1 has similar
properties. Mathemagics are nifty :)
>
> Here is a little routine I wrote a hell of a long time ago for this
> purpose.
>
> sub LUHN10 {
> my ($a, @cc, $sum);
> my ($cc = @_[0]);
> $cc =~ tr/0-9//cd;
> @cc = split('', $cc);
> while (@cc) {
> $sum += pop(@cc);
> $a = pop(@cc) * 2;
> $a -= 9 if $a > 9;
> $sum += $a;
> }
> return (!($sum % 10));
> }
>
> used as such:
>
> LUHN10('visa: 4444444444445555');
> LUHN10('4444 4444 4444 5555');
>
> It doesn't care about any non digit chars.
>
Two thoughts:
1. my ( $cc = @_[0] ); doesn't compile.
btw, you mean $_[0], or shift(@_), but not @_[0].
2. sort() uses magical variables $a and $b, so it's a good
idea to stay away from them.
> <rant>
> And as such, I'm always disgusted by commerce sites that require me to
> leave the spaces out of my credit card number. It shouldn't matter one
> iota to the computer, but it sure makes it easier for me to see if I've
> made a typo when I can put spaces in there.
> </rant>
>
I agree. Rant at Jennifer, she's writing an e-commerce site right now ;)
Belden
> Have fun.
>
> -Dave
More information about the Oakland
mailing list