Algori...ase_001(3pm)

πŸ“¦
version 1.3.6 Β· 5 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– CheckDigits::MBase_001 β€” the UPC check digit girlie πŸ’–

okay so bestie, this is a 3pm manual page (that's a Perl module doc, iykyk) and it's literally all about computing check digits for UPC (that's the barcode thing on everything in America πŸ‡ΊπŸ‡Έ). It lives in the Algorithm::CheckDigits family. She's small, she's focused, she's iconic. Let's go ✨


🌸 NAME 🌸

CheckDigits::MBase_001 β€” compute check digits for UPC (US) πŸ›’


✨ SYNOPSIS ✨

use Algorithm::CheckDigits;

$rv = CheckDigits('upc');

if ($rv->is_valid('012345678905')) {
      # do something
}

$cn = $rv->complete('01234567890');
# $cn = '012345678905'

$cd = $rv->checkdigit('012345678905');
# $cd = '5'

$bn = $rv->basenumber('012345678905');
# $bn = '01234567890'

like, that's the whole vibe fr fr β€” you grab the 'upc' checker, then you poke at it with a number and she tells you things πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

πŸŽ€ ALGORITHM πŸŽ€

no cap, here's the whole recipe, step by step, and you're gonna follow it in order:

  1. Add all digits in odd-numbered positions.
  2. Multiply the sum from step 1 with 3.
  3. Add all digits in even-numbered positions.
  4. Add the product from step 2 and the sum from step 3.
  5. If the sum from step 4 is 0 modulo 10, the check digit is 0. Else the check digit is 10 minus the sum from step 4 taken modulo 10.

so yeah, multiply by 3, mod by 10, and if you land on 0 the digit is literally just 0. slay πŸ”₯

🧚 METHODS 🧚

  • is_valid($number) β€” she only returns true if $number is made up of nothing but numbers and the last digit is a valid check digit according to the algorithm up there ☝️. Any other situation? Returns false. periodt.
  • complete($number) β€” she computes the check digit for $number and concatenates it onto the end of $number. You get back the complete number with its check digit β€” OR, if $number isn't solely digits and spaces, you get ''. rude but fair πŸ™ˆ
  • basenumber($number) β€” if $number has a valid check digit, she gives you back the basenumber of $number. Otherwise? ''. that's it, that's the whole deal.
  • checkdigit($number) β€” if $number has a valid check digit, you get the checkdigit of $number. Otherwise ''. she said no πŸ’€

πŸ‘‘ EXPORT πŸ‘‘

None by default. nothing, nada, zip. you want stuff? you ask for it. πŸ’…


🌈 AUTHOR 🌈

Mathias Weidner, "<mamawe@cpan.org>" β€” the icon who wrote this 🫢


πŸ“ THANKS πŸ“

Aaron W. West pointed me to a fault in the computing of the check digit. shoutout to him for the bug catch, real ones only ⭐


πŸ”₯ SEE ALSO πŸ”₯

perl, CheckDigits, www.pruefziffernberechnung.de, www.export911.com/e911/coding/upcChar.htm, www.adams1.com/pub/russadam/upccode.html, http://www.upcdatabase.com.


bestie reminder: is_valid / complete / basenumber / checkdigit all quietly return '' (empty string) instead of throwing a fit when the input isn't up to standard. don't be surprised when you get nothing back 😭

okay bye ily, this was perl v5.36.0, dated 2022-10-13, and it's the Algori...ase_001(3pm) page β€” go forth and validate some barcodes πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from