Dutch bank-account numbers must pass the "elf-proef" (eleven-test) to be valid. This sub tests if a number is a valid account number.
sub elfProef ($) { my ($account) = @_; die("elfproef: `$account' isn't 9 characters long\n") unless length($account) == 9; my $sum = 0; foreach (reverse(-9..-1)) { $sum += (substr($account, $_, 1) * abs($_)); } return not ($sum % 11); }

Replies are listed 'Best First'.
Re: Elfproef (Dutch bank account check)
by CheeseLord (Deacon) on Jul 28, 2001 at 22:40 UTC

    This might just be a matter of taste, but I wouldn't die if the string wasn't 9 digits long; instead, I'd return false, as it clearly is an invalid number. I'm a bit wary of having subroutines cause my program to die. Then again, I don't know the context of the sub in your program. :-)

    Also, if you're going to check the input for validity, you might also test that the string contains all digits; /^\d{9}$/ would take care of both conditions. Just some things to think about.

    His Royal Cheeziness

Re: Elfproef (Dutch bank account check)
by petral (Curate) on Jul 27, 2001 at 01:52 UTC
    Not that it matters unless I'm missing something, but why not:
    foreach (1..9)) { $sum += (substr($account, $_-1, 1) * $_); }
    ?

      p

      Because you have to multiply the last number by 1, the second to last by 2, etc.
      $sum += (substr($account, $_-1, 1) * $_);
      multiplies the first by 1, the second by 2 (the wrong way around :)
        Oh, right.  I guess the only simplification would be
        $sum += substr($account, -$_, 1) * $_;
        Anyway, good code.

          p