Update: Oops... This post was intended as a reply to this post, not to LanX. | Reparented.
Some general thoughts on "this monster" (if you feel you need a function at all):
sub sdtt() {
#sum of digits on Text number
local $t =0;
map { $t+= $_ } split //, sprintf("%d",$_[0]);
return $t;
}
-
sub sdtt() { ... } Don't define a subroutine with a prototype that requires the subroutine to take no arguments and then call the subroutine with an argument. I suspect you are calling the subroutine in a way that defeats
prototyping | prototype checking, e.g.
&sdtt($n);
or some such. Please don't use prototypes unless you know exactly what they do. Please see Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen. If you use prototypes, please don't defeat them without good reason.
-
local $t = 0; Please don't local-ize package-global variables in subroutines (unless you really need to). You'll give yourself a headache. Use my (lexical) variables instead.
-
In this particular case, you don't even need a local or a lexical variable. Using List::Util::sum() makes things a bunch simpler:
c:\@Work\Perl\monks>perl -wMstrict -le
"use List::Util qw(sum);
;;
sub sdtt { return sum split //, $_[0]; }
;;
for (@ARGV) {
print qq{sum of digits of $_ == }, sdtt($_);
}
" 321023 321 023
sum of digits of 321023 == 11
sum of digits of 321 == 6
sum of digits of 023 == 5
(split implicitly stringizes its EXPR argument.) (Update: List::Util has been core since Perl version 5.8. Present in more recent versions of this module, the function sum0() may be more useful to you since it returns a sum of 0 for an empty string.)
Update: And another thing...
map { $t+= $_ } split //, sprintf("%d",$_[0]);
This is a philosophical nit I'm picking, but I would use for in this type of loop:
$t += $_ for split //, ...;
I still don't understand why anyone would prefer to use map in a situation like this, i.e., purely for its side-effect(s).
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
Thanks - guidance much appreciated. The sub() {.. definition } just an old bad habit.
| [reply] |
I'm still not sure what you are trying to achieve, but sprintf can also eliminate leading zeroes.
DB<8> printf "%d","023"
23
| [reply] [d/l] |