Not splitting pays time dividends:

sub checkUPCGrandFather2 { my $str = shift; return "should be 12 digits in length" if length ($str) != 12; my $t = 0; my $sum = 0; # loop through to sum even and adjusted odd chars for (0..10) { my $chr = substr $str, $_, 1; $sum += ($t ^= 1) ? $chr * 3 : $chr; } # calculate correct check digit my $check = (10 - $sum % 10) % 10; # return error message if wrong check digit was initially given return ($check != substr $str, 11) ? "invalid checkdigit...should +be $check" : 'Ok'; }

gives the following improvement:

checkUPCGrandFather1: invalid checkdigit...should be 6 checkUPCGrandFather2: invalid checkdigit...should be 6 checkUPCroboticus: invalid checkdigit...should be 6 checkUPCEvanK: invalid checkdigit...should be 6 Rate GrandFather1 EvanK roboticus GrandFathe +r2 GrandFather1 21630/s -- -11% -19% -7 +2% EvanK 24183/s 12% -- -9% -6 +9% roboticus 26595/s 23% 10% -- -6 +5% GrandFather2 76965/s 256% 218% 189% +--
use warnings; use strict; use Benchmark qw(cmpthese); print 'checkUPCGrandFather1: ' . checkUPCGrandFather1('064200115897') +. "\n"; print 'checkUPCGrandFather2: ' . checkUPCGrandFather2('064200115897') +. "\n"; print 'checkUPCroboticus: ' . checkUPCroboticus('064200115897') . "\n" +; print 'checkUPCEvanK: ' . checkUPCEvanK('064200115897') . "\n"; cmpthese (-1, { GrandFather1 => sub {checkUPCGrandFather1 ('064200115897')}, GrandFather2 => sub {checkUPCGrandFather2 ('064200115897')}, roboticus => sub {checkUPCroboticus ('064200115897')}, EvanK => sub {checkUPCEvanK ('064200115897')}, } ); sub checkUPCEvanK { # grab and immediately split upc into array, 1 char per element my @chars = split(//, shift); # return error message if incorrect length if( $#chars != 11 ) { return "should be 12 digits in length"; } my ($odd, $even); # loop through to seperately sum even and odd chars foreach (0..10) { if($_ % 2 == 0) { $odd += $chars[$_]; } else { $even += $chars[$_]; } } # calculate correct check digit my $mult = my $check = ($odd * 3) + $even; while($mult % 10 != 0) { $mult++; } $check = $mult-$check; # return error message if wrong check digit was initially given if($check != $chars[11]) { return "invalid checkdigit...should be $check"; } # otherwise, if validated, return undefined return; } # returns error message on failure, undefined value on success sub checkUPCroboticus { # grab and immediately split upc into array, 1 char per element my @chars = split //, shift; # return error message if incorrect length return "should be 12 digits in length" if $#chars != 11; my ($even, $odd, $check); foreach (0..5) { $odd += shift @chars; $check = shift @chars; $even += $check; } my $mult = 3*$odd+$even-$check; my $chk2 = 10 - ($mult%10); return "invalid checkdigit...should be $chk2" if $check!=$chk2; } sub checkUPCGrandFather1 { # grab and immediately split upc into array, 1 char per element my @chars = split //, shift; return "should be 12 digits in length" if @chars != 12; local $| = 0; my $sum = 0; # loop through to sum even and adjusted odd chars $sum += $chars[$_] * (--$| ? 3 : 1) foreach 0..10; # calculate correct check digit my $check = (10 - $sum % 10) % 10; # return error message if wrong check digit was initially given return $check != $chars[11] ? "invalid checkdigit...should be $che +ck" : undef; } sub checkUPCGrandFather2 { my $str = shift; return "should be 12 digits in length" if length ($str) != 12; my $t = 0; my $sum = 0; # loop through to sum even and adjusted odd chars for (0..10) { my $chr = substr $str, $_, 1; $sum += ($t ^= 1) ? $chr * 3 : $chr; } # calculate correct check digit my $check = (10 - $sum % 10) % 10; # return error message if wrong check digit was initially given return ($check != substr $str, 11) ? "invalid checkdigit...should +be $check" : 'Ok'; }

DWIM is Perl's answer to Gödel

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.