Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Remove non-numerics from string

by Anonymous Monk
on Jan 26, 2004 at 17:47 UTC ( [id://324194]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks , I have a veriable that contains the following :
$oldValue = "UPNOON_0303.doc";
I need to get the number in oldValue and set it to a new value my new value should look like this :
$newValue = "0303";
I tried split but not sure if it is the righ way , can some one help :
my $newValue=split("_",$oldValue,2);

janitored by ybiC: Rename from less-than-descriptive "regx issue"

Replies are listed 'Best First'.
Re: Remove non-numerics from string
by Roy Johnson (Monsignor) on Jan 26, 2004 at 18:11 UTC
    $oldValue =~ tr/0-9//dc will get rid of all non-digits.

    The PerlMonk tr/// Advocate
Re: Remove non-numerics from string
by svsingh (Priest) on Jan 26, 2004 at 18:02 UTC
    I think you're on the right track, but probably don't want to use split. Here, split would take the string and break it into a list of strings after each underscore.

    For what you're trying to do, a simple regular expression match will work. If you just want to pull out the sequence of numbers from $oldValue, then try something like:

    if ( $oldValue =~ m|(\d+)| ) { $newValue = $1; }

    For what you're trying to do with split, you need to specify which list element you want to put into $newValue. Since the list starts at element 0, you'll want element 1. You can access it with the following statement:

    $newValue=(split("_",$oldValue,2))[1]

    Note, this will return everything after the underscore, and not just the numbers.

Re: Remove non-numerics from string
by captain_haddock (Novice) on Jan 26, 2004 at 19:51 UTC
    if all your values are of the form XXX_DDDD.doc, then you can use
    $oldValue =~ /_(\d+)\./; $newValue = $1;
      It's a good idea to always check if a regex succeeds before using $1 and friends, or you may get a result from a previous match. One way to do this implicitly: ($newValue) = $oldValue =~ /_(\d+)\./; This leaves $newValue as undef on failure (because the match returns an empty list). You can even set newValue and check the result all at once: ($newValue) = $oldValue =~ /_(\d+)\./ or warn "bad format: '$oldValue'"; or
      if (($newValue) = $oldValue =~ /_(\d+)\./) { # all is ok } else { # something went wrong }
Re: Remove non-numerics from string
by Not_a_Number (Prior) on Jan 26, 2004 at 20:13 UTC

    Your question is not very clear. All of the solutions offered above assume, more or less, that you want to remove any non-numeric characters from your string. However, if what you really want is to remove everything up to and including the underscore, regardless of whether it's numeric or whatever, you could do this:

    use strict; use warnings; my $old_value = 'UPNOON33_A303.doc'; my $new_value = (split /_/, $old_value)[-1]; print $new_value;

    hth

    dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found