UPC: Difference between revisions
Content added Content deleted
Catskill549 (talk | contribs) |
(→{{header|REXX}}: added the REXX computer programming language for this task.) |
||
Line 715: | Line 715: | ||
653483540435</pre> |
653483540435</pre> |
||
=={{header| |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program to read/interpret UPC symbols and translate them to a numberic string.*/ |
|||
<lang zkl>var lhd=Dictionary(), rhd=Dictionary(); |
|||
#.0= ' ## #' |
|||
[0..].zip(List( |
|||
#.1= ' ## #' |
|||
#.2= ' # ##' |
|||
#.3= ' #### #' |
|||
#.4= ' # ##' |
|||
#.5= ' ## #' |
|||
#.6= ' # ####' |
|||
#.7= ' ### ##' |
|||
#.8= ' ## ###' /* [↓] right─sided UPC digits.*/ |
|||
"0 1 1 1 0 1 1", |
|||
#.9= ' # ##' ; do i=0 for 10; ##.i= translate(#.i, ' #', "# ") |
|||
"0 1 1 0 1 1 1", |
|||
end /*i*/ |
|||
say center('UPC', 14, "─") ' ___'copies(1234567, 6)"-----"copies(1234567, 6)'___' |
|||
).pump(Void,fcn([(n,bs)]){ |
|||
@.=. |
|||
bs-=" "; |
|||
@.1 = ' # # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # # ' |
|||
lhd[bs.translate("01","_#")]=n; |
|||
@.2 = ' # # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # # ' |
|||
rhd[bs.translate("10","_#")]=n; |
|||
@.3 = ' # # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # # ' |
|||
}); |
|||
@.4 = ' # # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # # ' |
|||
@.5 = ' # # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # # ' |
|||
@.6 = ' # # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # # ' |
|||
@.7 = ' # # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # # ' |
|||
@.8 = ' # # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # # ' |
|||
@.9 = ' # # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # # ' |
|||
@.10= ' # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # ' |
|||
do j=1 while @.j\==.; $= @.j; txt= |
|||
fcn parseBarCode(barcode, one=True){ // --> 12 digits |
|||
if left($, 9)\='' | right($, 9)\='' then txt= 'bad blanks' |
|||
upsideDown:='wrap{ // was I looking at this bar code upside down? |
|||
$= strip($); $$= $; L= length($) |
|||
if(one and (r:=parseBarCode(barcode.reverse(),False))) return(r); |
|||
if left($, 3)\=='# #' | right($, 3)\=="# #" then txt= 'bad fence' |
|||
return(False); |
|||
if L\==95 & txt=='' then txt= 'bad len' |
|||
}; |
|||
$= substr($, 4, L - 3 - 3); sum= 0 /*elide ends.*/ |
|||
$= delstr($, length($) % 2 - 1, 5) /* " middle.*/ |
|||
var [const] start=RegExp(String("_"*9, "+#_#")), tail="_"*7; |
|||
if txt=='' then do k=1 for 12; parse var $ x +7 $ /*get UPC dig.*/ |
|||
if(not start.search(barcode)) return(upsideDown()); |
|||
do d=0 for 10; if x==#.d | x==##.d then leave /*valid digit?*/ |
|||
r,idx,d,mark := List(), start.matched[0][1], lhd, "_#_#_"; |
|||
end /*d*/ |
|||
do(2){ |
|||
if d==10 & k\==12 then do; txt= 'reversed'; leave; end |
|||
do(6){ |
|||
if d==10 then do; txt= 'bad dig'; leave; end |
|||
if(Void==(z:=d.find(barcode[idx,7]))) return(upsideDown()); |
|||
if k//2 then sum= sum + 3 * d /*mult. by 3.*/ |
|||
r.append(z); |
|||
else sum= sum + d /* " " 1.*/ |
|||
idx+=7; |
|||
txt= txt || d |
|||
} |
|||
end /*k*/ |
|||
if(barcode[idx,5] != mark) return(Void); |
|||
if sum//10 \== 0 then txt= 'bad chksum' /*invalid sum?*/ |
|||
d,idx,mark = rhd, idx+5, "#_#__"; |
|||
say center( strip(txt), 15) ' ' $$ |
|||
} |
|||
end /*j*/ /*stick a fork in it, we're all done. */</lang> |
|||
if(tail!=barcode[idx,7]) return(Void); // 9 trailing blanks? two checked above |
|||
{{out|output|text= when using the internal default input:}} |
|||
r |
|||
}</lang> |
|||
Or, if you like way too long regular expressions: |
|||
<lang zkl>var upcRE = RegExp(String("_"*9, "+#_#", |
|||
lhd.keys.concat("|","(",")")*6, "_#_#_", |
|||
rhd.keys.concat("|","(",")")*6, "#_#", "_"*9)), |
|||
digits=lhd.copy().extend(rhd); |
|||
fcn parseBarCode(barcode){ // --> 12 digits |
|||
if(not (upcRE.search(barcode) or upcRE.search(barcode.reverse()))) return(False); |
|||
upcRE.matched[1,*] // ( (a,b), "_#_####","_##___#", 10 more digit patterns ) |
|||
.apply(digits.get) |
|||
}</lang> |
|||
<lang zkl>barcodes:= |
|||
#<<<" |
|||
_________#_#___#_##__#__##_#___##_###_##_###_##_####_#_#_#_##_##__#___#__##__##_###__#_##__##_###_#__#_#_________ |
|||
_________#_#_#___##___##_#_####_#___#_##_#___##_#___##_#_#_#_###__#_###__##__##_###__#_#__###_###__#_#_#_________ |
|||
_________#_#____#_#_#__###__#___#____#_#__#___#____#_#_#_#_##_#___##_#___##_#___##___#_#_####_###_##_#_#_________ |
|||
_________#_#_##__##_##__##___#__#___#__#_###__#_##__##_#_#_#___##_##__#__###_##_##_#___#_####_##_#___#_#_________ |
|||
_________#_#_###_##_#___##_##_###__##__#_##___#___#_##_#_#_###_#__##_##__#____#_###_#__##_##__#______#_#__________ |
|||
__________#_#__#___#_##__##__#___#___#__#_##__##__#___#_#_#_#_####_#__##__#_####_####_#_#__##__#_####_#_#____________ |
|||
_________#_#__#__##__##__#_#___##_##___#_###_##_##___#_#_#_#__#___#___#__#__###_#_#____###__#_#__#___#_#_________ |
|||
_________#_#_#____#_##__##___#__#_##__##__###_#___#__#_#_#_###_##_##_###_##_###_###_##_#__##__###_##_#_#__________ |
|||
_________#_#_###_##___##_#_#_####_#___##_#_####_#_####_#_#_#___#__#_###__#____#_###__#_#____#_###__#_#_#_________ |
|||
_______________#_#_#_####_##___#_####_#_#___##_##_###_####_#_#_#_#__###_#_###__###__#_#_###__#____#_#__###_#_#_________" |
|||
.split("\n"); |
|||
#<<< |
|||
foreach n,barcode in ([1..].zip(barcodes)){ |
|||
bc:=parseBarCode(barcode); |
|||
println("%2d: [%s]".fmt(n,bc and bc.concat(" ") or "Not valid")); |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
<pre> |
||
─────UPC────── ___123456712345671234567123456712345671234567-----123456712345671234567123456712345671234567___ |
|||
1: [9 2 4 7 7 3 2 7 1 0 1 9] |
|||
924773271019 # # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # # |
|||
2: [4 0 3 9 4 4 4 4 1 0 5 0] |
|||
403944441050 # # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # # |
|||
3: [8 3 4 9 9 9 6 7 6 7 0 6] |
|||
reversed # # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # # |
|||
4: [9 3 9 8 2 5 1 5 8 8 1 1] |
|||
reversed # # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # # |
|||
5: [Not valid] |
|||
bad dig # # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # # |
|||
6: [3 1 6 3 1 3 7 1 8 7 1 7] |
|||
reversed # # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # # |
|||
7: [2 1 4 5 7 5 8 7 5 6 0 8] |
|||
214575875608 # # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # # |
|||
8: [8 1 8 7 7 8 8 4 1 8 1 3] |
|||
reversed # # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # # |
|||
9: [7 0 6 4 6 6 7 4 3 0 3 0] |
|||
706466743030 # # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # # |
|||
10: [6 5 3 4 8 3 5 4 0 4 3 5] |
|||
bad len # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # |
|||
</pre> |
</pre> |