in reply to Re: How to Switch off octal interpretation of literal ?
in thread How to Switch off octal interpretation of literal ?
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; }
(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.)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
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: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: How to Switch off octal interpretation of literal ?
by gannett (Novice) on Apr 08, 2018 at 09:32 UTC |