Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Strip non-numeric

by FireBird34 (Pilgrim)
on Jan 13, 2003 at 22:47 UTC ( [id://226634]=perlquestion: print w/replies, xml ) Need Help??

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

I need to strip all non-numeric characters within a scalar. I can do it with the output being stored in an array, but that's about it. Any ideas? This is the code I had:
$scalar = "a1b2"; @array = split(/\D/,$scalar); print @array;
Also, talking with someone else (PHP they use), and they commented about using regex when doing what I need. Can someone enlighten me on using this to strip the non-numeric values? Thanks.

Replies are listed 'Best First'.
Re: Strip non-numeric
by ihb (Deacon) on Jan 13, 2003 at 22:55 UTC
    Sure, you can use a substitution:   s/\D//g; # global or you can use a special transliteration:   tr/0-9//cd; # complement, delete A more elaborate description is found in the perlop document.

    I must here point out that s/// and tr/// are two totally different operators! As said, they're documented in perlop.

    Other documents you might want to read are the perlre* documents. All perldocs are listed in the document "perl" (perldoc perl).

    Hope I've helped,
    ihb
      Thanks all -- got it :-) I ended up using:
      $scalar = "a1b2"; $scalar =~ s/\D//g; print $scalar;
      Also, I'll take a look at the perl documents on the s/// tr/// differences. Thanks.
        You should consider using s/\D+//g, as that's often a lot faster than s/\D//g. Here's a benchmark:
        #!/usr/bin/perl use strict; use warnings; use Benchmark; my @sizes = (10, 25, 50, 100, 250, 500, 1000); my @chars = ('A' .. 'Z', 'a' .. 'z', 0 .. 9); our @d = map {join "" => map {$chars [rand @chars]} 1 .. $_} @sizes; map { Benchmark::cmpthese timethese (-2 => { "simple_$sizes[$_]" => '$_ = $::d[' . $_ . ']; s/\D//g;', "multiple_$sizes[$_]" => '$_ = $::d[' . $_ . ']; s/\D+//g;' }, 'none'); } 0 .. $#sizes __END__ Rate simple_10 multiple_10 simple_10 196495/s -- -15% multiple_10 231225/s 18% -- Rate simple_25 multiple_25 simple_25 89788/s -- -50% multiple_25 180650/s 101% -- Rate simple_50 multiple_50 simple_50 47507/s -- -64% multiple_50 130727/s 175% -- Rate simple_100 multiple_100 simple_100 23206/s -- -77% multiple_100 103096/s 344% -- Rate simple_250 multiple_250 simple_250 10488/s -- -71% multiple_250 36407/s 247% -- Rate simple_500 multiple_500 simple_500 5046/s -- -75% multiple_500 20382/s 304% -- Rate simple_1000 multiple_1000 simple_1000 2528/s -- -76% multiple_1000 10549/s 317% --

        Abigail

Re: Strip non-numeric
by krujos (Curate) on Jan 13, 2003 at 22:50 UTC

    Try this  $var =~ s/\D//g; This (untested) code will remove all non-numeric characters from the string.

    Josh
    Update: I fixed my type where I left the g off the regexp. Sorry
    Update:2 I also fixed the /// problem that tall_man pointed out. Thanks.
      There's one extra "/" at the end, and no "g" flag so only one would be removed. You need:
      $var =~ s/\D//g;
      Update: The "g" was just fixed in the message I replied to, but not the extra "/"
      Update 2:Now all the problems are fixed. :)
Re: Strip non-numeric
by rbc (Curate) on Jan 13, 2003 at 22:52 UTC
    try ...
    $scalar = "a1b2"; $scalar =~ s/\D//g; print "$scalar\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://226634]
Approved by krujos
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2024-04-18 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found