[oak perl] Customizing a Barcode Checkdigit Subroutine

Belden Lyman blyman at iii.com
Fri Dec 20 16:01:41 CST 2002


There's only one mod (%) in this, so it really zips along.
If speed is really a concern, re-write it in Inline::C. That's
left as an exorcise for the reader :)

sub checkdigit {
   my $bar = shift;
   length $bar == 13 or do { warn "$bar not 13 digits long, skipping\n";
                             return undef; };

   my $sum;
   for ( 0, 2, 4, 6, 8, 10, 12 ) {         # all odd positions
     my $num = substr($bar, $_, 1) * 2;
     $sum += $num > 9 ? $num - 9 : $num ;  # magic 9 :)
   }

   $sum += substr $bar, $_, 1 for ( 1, 3, 5, 7, 9, 11 );

   my $check = 10 - $sum % 10;
   $check = 'X' if $check == 10;
   return $check;
}


Larry Currie wrote:
> Belden,
> 
> The 12 and the 16 are to be added as 1 + 2 and 1 + 6. ;).
> 
> Larry
> 
> 




More information about the Oakland mailing list