in reply to ABN checker

To reduce surprise, the function should return true if the number is valid, and false otherwise, which is the other way round.

There's no need to backslash a space in regular expressions. Moreover, there's no need for a substitution in this case, transliteration does the same job (and is simpler and faster).

There's a pattern in the computation of the sum which could be expressed by a for loop.

See my version below:

sub check_abn { my ($number) = @_; $number =~ tr/ //d; return if 11 != length $number; my $sum = 0; $sum += (2 * $_ - 1) * substr $number, $_, 1 for 1 .. 10; $sum += 10 * (substr($number, 0, 1) - 1); return $sum % 89 == 0 }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: ABN checker
by bgroper (Novice) on Feb 06, 2019 at 11:23 UTC
    Thanks for your suggestions, especially to reverse the logic and reduce surprise.
      I like the code from choroba, he posted that code while I was still writing mine.

      Some notation comments: Instead of "ChkABN", I would use "isABN". That is a common convention that makes it clear that this is a yes/no question, usually >0 or 0 for return value (any non zero value is "true").

      I would reserve any variable name that starts with a capital letter for a Class.

      I personally like the style that choroba use for naming (all lower case and _ for separation). is_ABN() would be a good name for me.