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

Is there a slick way to get just the a-z{3} chars from this?

my @eFILES = grep { (!/^\./) && (/^env\.[a-z]{3}$/) && (!/^env\.fix$/) +&& -f "$LIBDIR/$_" } readdir(LDIR);

Or do I need to put it thru a loop to remove "env." Thanx

Replies are listed 'Best First'.
Re: grep & take part of match
by toolic (Bishop) on Jun 28, 2012 at 20:13 UTC
    Putting the extensions in another array (but kinda ugly):
    my @exts; my @eFILES = grep { /^env\.([a-z]{3})$/; push @exts, $1 if $1 && ($_ ne 'env.fix') && -f "$LIBDIR/$_" } readdir(LDIR);

    Note that your 1st regex is not needed ((!/^\./)) because you are also checking for env at the start of the string.

    Updated to make a little more readable.

Re: grep & take part of match
by Perlbotics (Archbishop) on Jun 28, 2012 at 21:03 UTC

    Another option is to use the indirect loop of/within map:

    my @eFILES = map { substr($_, -3) } grep { (!/^\./) && (/^env\.[a-z]{3}$/) && (!/^env\.fix$ +/)&& -f "$LIBDIR/$_" } readdir( LDIR );

Re: grep & take part of match
by jwkrahn (Abbot) on Jun 29, 2012 at 01:24 UTC
    my @eFILES = map -f "$LIBDIR/$_" && !/^env\.fix$/ && /^env\.([a-z]{3}) +$/ ? $1 : (), readdir LDIR;