sub cd_AM_HOP { # iterator solution my ($n_digits, # n of digits for which to generate luhn checksum ) = @_; my $order = $n_digits & 1; my @straight = grep +($_ & $order), 0 .. $n_digits-1; # dd '---', \@straight; my @adjusted = grep !($_ & $order), 0 .. $n_digits-1; # dd '===', \@adjusted; my @check_digit = map { (10 - $_ % 10) % 10 } 0 .. 9 * $n_digits; return sub { my $ccn = shift; # digits for which to generate checksum my $total = 0; for (@straight) { $total += substr($ccn, $_, 1); } for (@adjusted) { $total += $x2_cast_out_9[ substr($ccn, $_, 1) ]; } # return $check_digit[ $total ]; # return check digit $total *= 9; return chop $total; # return check digit }; # end sub iterator }