wusphere has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, How do we count the number of digits in a numeric value. For example: $num - 123; so the total digit count would be 3

Replies are listed 'Best First'.
Re: count digit in a numeric value
by Zaxo (Archbishop) on Aug 15, 2004 at 04:11 UTC

    my $digit_count = $num =~ tr/0-9//;

    After Compline,
    Zaxo

      Thank you Monks!
Re: count digit in a numeric value
by hsinclai (Deacon) on Aug 15, 2004 at 04:09 UTC


    with length()..
    $str=123; $fu = length($str); print $fu,"\n";

    hm..
    To count the digits in an alphanum string..
    perl -e '$str="fff123"; $fu = ($str =~ tr/[a-z]//c); print $fu,"\n";'
    I know, it's not what you asked, just trying to complicate things needlessly:)

    updated, pls see following reply
      Thanks
      wusphere pay no attention to the one liner above, it's wrong, and off the point of your original question, as was pointed out offline .. brackets don't belong in tr///, should be looking for digits not anything else like a-z ..
      perl -e '$str="fff123"; $fu = $str =~ tr/0-9//c; print $fu,"\n";'

      Which is what Zaxo said anyway.
Re: count digit in a numeric value
by johnnywang (Priest) on Aug 15, 2004 at 05:02 UTC