Skip Menu |

This queue is for tickets about the Business-UPC CPAN distribution.

Report information
The Basics
Id: 97408
Status: resolved
Priority: 0/
Queue: Business-UPC

People
Owner: Nobody in particular
Requestors: perl [...] xev.net
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Incorrect check digit calculation
It looks like you are calculating the check digit incorrectly. You are multiplying each even numbered digit by 3 instead of first summing them all together and multiplying the final result by 3. Here is an example of how I understand it to work. It has worked with all of the UPCs I have tested it with. sub _check_digit { my ($upc) = @_; my @digits = split(//, $upc); my $odd = 0; my $even = 0; foreach my $i (0, 2, 4, 6, 8, 10) { $odd += $digits[$i] || 0; $even += $digits[$i+1] || 0; } my $res = $odd * 3 + $even; my $check = 10 - ($res % 10); return $check; }
Sorry, I completely left off the special case of the check digit being 0 if the remainder is 10. The below fixes that. sub upc_check_digit { my ($upc) = @_; my @digits = split(//, $upc); my $odd = 0; my $even = 0; foreach my $i (0, 2, 4, 6, 8, 10) { $odd += $digits[$i] || 0; $even += $digits[$i+1] || 0; } my $res = $odd * 3 + $even; my $check = 10 - ($res % 10); $check = 0 if $check == 10; return $check; }
Sorry..I guess I should be calling fix_check_digit first.