0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

連番バーコード

Posted at

7桁連番 + modulus 10, weight 3 のチェックディジットのバーコードの単純計算

コード

メインの部分を。

$ pbpaste
package BARCODE;
sub _ad  { eval join q{+}, @_ }
sub _cut { ( split//, shift @_ )[0..6]}
sub _fmt { sprintf "%07d", substr $_[0], 0, 7 }
sub _mg  { join '', @_ }
sub _init {
    my $self = shift ;
    $self->{barcode} = _fmt $self->{barcode}  ;
    my @d = _cut $self->{barcode}  ;
    $self->{barcode} = _mg +( @d, ( (  10 - ( ( _ad @d[0,2,4,6] ) * 3 + _ad @d[1,3,5] ) % 10 ) % 10 )  ) ;
    return $self ;
}
sub new{ bless _init { barcode => $_[1] || "0000000" } , $_[0]  }
sub add{
    my $self = shift ;
    $self->{barcode} = _fmt 1 + _mg _cut $self->{barcode} ;
    return _init $self ;
}
1;

my $o = BARCODE->new(100) ;
print $o->{barcode} ;
print $o->add->{barcode} ;
print $o->add->{barcode} ;

実行

$ pbpaste | perl -l
00001007
00001014
00001021
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?