in reply to Grabbing file prefixes with Reg Exp

Allow me to point out some places of improvement: That said, I offer:
my @files = map s/\.[Rr][Aa][Mm]$// ? $_ : (), readdir DH;
Maybe you think those character classes look ugly, and maybe the difference is minimal, but in a case like this, I think it's a wise investment. Also, you may wish to use a different ending anchor in place of $.

japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re: Re: Grabbing file prefixes with Reg Exp
by dws (Chancellor) on May 14, 2001 at 00:03 UTC
    using /i is sometimes wasteful, especially for a few characters

    <counteropinion>

    Unless the regexp is going to be in a performance-sensitive area of code, the performance gained by using character classes instead of /i is going to be outweighed by the readability hit.

    To my eye, it's easier (albeit only slightly so) to grasp the intent of   my @files = map s/\.ram$//i ? $_ : (), readdir DH; than it is to grasp   my @files = map s/\.[Rr][Aa][Mm]$// ? $_ : (), readdir DH; When hardware performance isn't an issue, optimize for wetware (readability).

Re: Re: Grabbing file prefixes with Reg Exp
by MeowChow (Vicar) on May 14, 2001 at 02:18 UTC
    Didn't you mean to make this a grep? :)
    my @files = grep s/\.ram$//i , readdir DH;
    Also, I agree with dws regarding the readability of the character classes, and believe you're altogether mistaken regarding the performance issue:
    use strict; use Benchmark qw(cmpthese); my $strorig = 'yabadabadooo this bud is a buda fora youa'; cmpthese (-3, { mod => sub { my $str = $strorig; $str =~ m/.*BUD.*/i }, class => sub { my $str = $strorig; $str =~ m/.*[Bb][Uu][Dd].*/ }, }); ## Results (under ActiveState 5.6) ## Rate class mod class 347719/s -- -15% mod 408606/s 18% --
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      Yes, it was meant to be a grep() -- I took the map() and modified it, and forgot to change it.

      And I guess I'm mistaken about char classes. I was using information from MRE, I think, which is now oldish, so Perl has probably made some changes.

      japhy -- Perl and Regex Hacker