Yes, sure. Some more inspiration might be found here. Sometimes it is good to take a look under a hood.
sub sumdigits { my($n,$base) = @_; my $sum = 0; $base = 2 if !defined $base && $n =~ s/^0b//; $base = 16 if !defined $base && $n =~ s/^0x//; if (!defined $base || $base == 10) { $n =~ tr/0123456789//cd; $sum += $_ for (split(//,$n)); } else { croak "sumdigits: invalid base $base" if $base < 2; my $cmap = substr("0123456789abcdefghijklmnopqrstuvwxyz",0,$base); for my $c (split(//,lc($n))) { my $p = index($cmap,$c); $sum += $p if $p > 0; } } $sum; }
From Math::Prime::Util::PP. List::MoreUtils::PP might be also of interest.
Regards, Karl
«The Crux of the Biscuit is the Apostrophe»
In reply to Re^3: Newbie question
by karlgoethebier
in thread Newbie question
by oldB51
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |