in reply to Strip non-numeric

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

Replies are listed 'Best First'.
Re: Strip non-numeric
by FireBird34 (Pilgrim) on Jan 13, 2003 at 22:58 UTC
    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

        In this case transliteration is really the most efficient solution though. Consider the results of adding         "xlit_$sizes[$_]"     => '$_ = $::d[' . $_ . ']; tr/0-9//cd;', to the benchmark:
        Rate simple_10 multiple_10 xlit_10 simple_10 86400/s -- -31% -70% multiple_10 124615/s 44% -- -57% xlit_10 292712/s 239% 135% -- Rate simple_25 multiple_25 xlit_25 simple_25 45324/s -- -49% -82% multiple_25 88062/s 94% -- -65% xlit_25 248802/s 449% 183% -- Rate simple_50 multiple_50 xlit_50 simple_50 23823/s -- -71% -89% multiple_50 82566/s 247% -- -62% xlit_50 218684/s 818% 165% -- Rate simple_100 multiple_100 xlit_100 simple_100 13397/s -- -69% -92% multiple_100 43191/s 222% -- -74% xlit_100 168434/s 1157% 290% -- Rate simple_250 multiple_250 xlit_250 simple_250 5608/s -- -71% -95% multiple_250 19639/s 250% -- -81% xlit_250 103656/s 1748% 428% -- Rate simple_500 multiple_500 xlit_500 simple_500 2832/s -- -72% -95% multiple_500 10189/s 260% -- -83% xlit_500 59072/s 1986% 480% -- Rate simple_1000 multiple_1000 xlit_1000 simple_1000 1380/s -- -77% -96% multiple_1000 5939/s 330% -- -83% xlit_1000 34457/s 2397% 480% --
        Esp in large data sets, transliteration screams.

        Makeshifts last the longest.

        Ok, thanks. Also did have one more question about this -- I didn't take into consideration about valid non-numeric (only decmial). So for example, 1a2b3.4c5d6e, I would want 123.456, not just 123456 -- tried a few combinations, but nothing yet. I'm missing something obvious I know ;) Any pointers?
        Basically, in any given string, I want only numeric and decimal values retained. So, basically:

        1a => 1
        1a.2b => 1.2
        1a.2b.3c => 1.2.3

        ^^ Just as those show, no matter how many decimals or numeric values, those are the only characters I want retained. Hope this clears it up just a bit.