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

Hi, I need a way of extracting the four numbers from this word, de0040Newark. I have about 400 of these words, and every word follows the same pattern ( 2 letters, 4 numbers, then the rest of the letters). I need a way of extracting the four numbers from that word. Thanks.

-Impala

Replies are listed 'Best First'.
Re: Splitting a word
by kilinrax (Deacon) on Oct 05, 2000 at 19:44 UTC
    Assuming all your data is in @array, then surely a regexp, along the lines of:
    for ($i=0; $i<=$#array; $i++) { $array[$i] =~ s/\w{2}(\d{4})\w+/$1/; }
    Would work?
      Or even:
      s/\w\w(\d{4})\w+$/$+/ for @array;
      Amazing... 5 mins after i post there is the answer that works.. thanks a million. :)

      -Dipul Patel
        You may be interested in reading perlre for basic regular expression processing. Another poster also mentioned substr, which is a standard core Perl function.
Re: Splitting a word
by wardk (Deacon) on Oct 05, 2000 at 20:05 UTC

    in the spirit of TIMTOWTDI...

    you could use the substring function

    this would assume a consistent format of 4 numbers beginning in position 3.

    #!/usr/bin/perl @servs = qw( de0040Newark de0050Oldark ); foreach (@servs) { print substr($_, 2, 4), "\n"; }
RE: Splitting a word
by Corion (Patriarch) on Oct 05, 2000 at 21:46 UTC

    In the spirit of even more TIMTOWTDI, here's the solution for unpack(), which is another good trick to have in your bag :

    $a = "de0400Newark"; print join( ",", unpack( "a2a4a*", $a ));
    Read in the manpage about how unpack() can help you to unpack structured data. Sometimes this is faster than using a regular expression, sometimes it is slower.

    I just noticed that you only want the number instead of all that data. So here's the code which will give you the number :

    $a = "de0400Newark"; print( (unpack( "a2a4a*", $a ))[1] );
Re: Splitting a word
by extremely (Priest) on Oct 06, 2000 at 06:50 UTC
    This is evil but...
    $a = "de0400Newark"; print split /[a-z]+/,$a;

    I'm embarassed to have posted that...

    But in case you were wondering:

                  Rate regex(Adam)       split      unpack      substr
    regex(Adam) 1554/s          --        -38%        -54%        -82%
    split       2504/s         61%          --        -25%        -70%
    unpack      3347/s        115%         34%          --        -60%
    substr      8446/s        444%        237%        152%          --
    

    Updated, I missed substr the first time, that would have been bad.

    --
    $you = new YOU;
    honk() if $you->love(perl)