in reply to Re: Summing the odd/even digits of a larger number (for upc check digits)...a better way?
in thread Summing the odd/even digits of a larger number (for upc check digits)...a better way?

Why use a special variable in an unintended and undocumented manner when you can use ($t^=1)?

Change
local $| = 0; $sum += $chars[$_] * (--$| ? 3 : 1) foreach 0..10;
to:
my $t = 0; $sum += $chars[$_] * (($t^=1) ? 3 : 1) foreach 0..10;

Update: sgifford improved this to:
my $t = 1; $sum += $chars[$_] * ($t^=2) foreach 0..10;

Update:
my $check = (10 - $sum % 10) % 10;
simplifies to
my $check = -$sum % 10;

  • Comment on Re^2: Summing the odd/even digits of a larger number (for upc check digits)...a better way?
  • Select or Download Code