Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

How to strip non-numeric values from string?

by jdlev (Scribe)
on Mar 19, 2010 at 19:14 UTC ( #829684=perlquestion: print w/replies, xml ) Need Help??
jdlev has asked for the wisdom of the Perl Monks concerning the following question:

I've got a bit of a quagmire. I'm trying to strip off unneccesary junk from data that I mine. Some of it has a "%" at the end. Some has stuff like "lb" or other characters in it. I want to strip all non-numeric characters from a string WITHOUT removing the decimal point. Any ideas on how to do this? Thanks monks! I can't use split because the data is inconsistent lengths.

I love it when a program comes together - jdhannibal
  • Comment on How to strip non-numeric values from string?

Replies are listed 'Best First'.
Re: How to strip non-numeric values from string?
by toolic (Bishop) on Mar 19, 2010 at 19:23 UTC
    One way is to use the substitution operator, s///. The following code will only keep digits and the decimal point. You should be able to modify the character class to keep other things, if needed.
    use strict; use warnings; for (qw(12.34% 0.05lb 999)) { my $s = $_; $s =~ s/[^\d.]//g; print "$s\n"; } __END__ 12.34 0.05 999
Re: How to strip non-numeric values from string?
by BrowserUk (Pope) on Mar 19, 2010 at 19:50 UTC
    tr[0-9.][]cd, print for @{[ qw[ 123.56% $12.34 13.50 12345MB ] ]};; 123.56 12.34 13.50 12345

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to strip non-numeric values from string?
by kennethk (Abbot) on Mar 19, 2010 at 20:12 UTC
    Number means a lot of things to a lot of people. One generally tricky bit to consider is scientific notation, i.e. "1.0e1". Fortunately, Perl is smarter than your average third-grader. If the unnecessary bit is only at the end of your string, you can simply use 0+ (the venus operator I think) to numify the result, and let Perl do the heavy lifting:

    my @array = (" 321abc ", "100%", "20 lbs."); no warnings "numeric"; print(0+$_,"\n") for @array;

    If you have leading chaff, you can run it through a substitution to strip that first:

    my @array = (" 321abc ", "100%", "20 lbs.", "words20", "**39.99.99** +"); no warnings "numeric"; for (@array) { s/^[^0-9.-]+//; print(0+$_,"\n"); }
      use strict; use warnings; for (qw(12.34 0.05lb 999)) { if($_=~/([0-9.]+)/) { print "$1\n"; } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://829684]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2018-03-20 01:00 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (247 votes). Check out past polls.