in reply to number of numbers in a string

The following should do:
$foo = "spider9832man23"; my @digits = ($foo =~ /\d/g); print scalar(@digits); # print 6
Looking at merlyn's Forcing list context, the above can be simplified as:
$foo = "spider9832man23"; print ( $count = () = $foo =~ /\d/g); # prints: 6

Replies are listed 'Best First'.
Re^2: number of numbers in a string
by ysth (Canon) on Aug 12, 2004 at 18:16 UTC
    # or: ++$count while $foo =~ /\d/g; # or: $count = $foo =~ s/(\d)/$1/g; # or: $count = () = $foo =~ /\d/g; # or: $foo =~ s/(\d)/++$count; $1/ge; # etc.
    But y/// (aka tr///) is the perfect-fit tool for this job.

    Update: s/gee/ge/; thanks, japhy; yes, a typo

      You have an extra /e modifier on your last regex, but it ends up being harmless. But it's still extraneous; while it might be a typo, it might also be a sign of a misunderstanding of its purpose.
      _____________________________________________________
      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart