... I'm trying to count digits in a scalar
That's what the OP says, but the /\d\D*/g regex suggests misterperl is trying to count occurrences of some kind of digit-group pattern. If that's the case, I can, offhand, think of some s/// approaches (in the vein of a tr/// operation) that would do the trick as well as the m// approach that misterperl seems to favor:
c:\@Work\Perl\monks>perl -wMstrict -le
"$_ = '+++1223w3433.45+34***';
;;
my $ndg;
;;
$ndg =()= /\d\D*/g;
print qq{A: $ndg digit grps.; m// change unpossible '$_'};
;;
$ndg = do { (my $r = $_) =~ s/\d\D*//g };
print qq{B: $ndg digit grps.; s/// string unchanged '$_'};
;;
$ndg = s/(\d\D*)/$1/g;
print qq{C: $ndg digit grps.; s/// string unchanged '$_'};
;;
$ndg = s/\d\D*//g;
print qq{D: $ndg digit grps.; s/// string CHANGED '$_'};
"
A: 12 digit grps.; m// change unpossible '+++1223w3433.45+34***'
B: 12 digit grps.; s/// string unchanged '+++1223w3433.45+34***'
C: 12 digit grps.; s/// string unchanged '+++1223w3433.45+34***'
D: 12 digit grps.; s/// string CHANGED '+++'
My own preference would be for the m// approach.
Give a man a fish: <%-{-{-{-<
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|