Anirban Adhikary wrote:
Hi list

Hello,

I have wirtten the following perl code which has some configuration
params such as prefix,suffix,midfix and nofix. The logic is filenames
will be selected based on prefix,suffix and midfix pattern.After the
fiter through above 3 patterns if file names exists with nofix
patterns it will be removed from the final list.

use strict;
use warnings;

#my $pfx ="CAT";
my $pfx = '';
my $sfx = '.Z' ;
my $mfx =   'r';
my $nfx= "dmp.Z,Q.Z,dat.Z,ctl.Z,A.Z";

Are these values supplied by you, in the program, or are they supplied by a user from outside the program?

If they are defined inside the program then you could just assign the regular expressions here.


my @fnames = 
("CAT_arcdf231456_dmp.Z","ABCD_1234.txt","FILE_STAT.Z","CAT_qwerty.Z");

my($re_pfx,$re_sfx,$re_mfx,$re_nfx);
my(@pre_fix,@sfx_fix,@mid_fix,@not_fix);

if((length($pfx))>  0 and $pfx !~ /^\s+$/) {

I would probably write that as:

if ( defined $pfx && $pfx =~ /\S/ ) {


        @pre_fix = split(/,/,$pfx);

This array only needs to be visible inside this scope so you should define it here instead of at file scope. Also, your string(s) may contain regular expression meta-characters so you need to quotemeta the string(s):

        my @pre_fix = map quotemeta, split /,/, $pfx;


        #find_largest_element(@pre_fix);
        $re_pfx  = join "|",@pre_fix;

If you are not using find_largest_element() then you don't really need the array:

        $re_pfx = join '|', map quotemeta, split /,/, $pfx;


        $re_pfx = qr/$re_pfx/;

A prefix only occurs at the start of a string so:

        $re_pfx = qr/\A$re_pfx/;


}
if((length($sfx))>  0 and $sfx !~ /^\s+$/) {
         @sfx_fix = split(/,/,$sfx);
         $re_sfx  = join "|",@sfx_fix;
         $re_sfx = qr/$re_sfx/;

A suffix only occurs at the end of a string so:

         $re_sfx = qr/$re_sfx\z/;


}

if((length($mfx))>  0 and $mfx !~ /^\s+$/) {
         @mid_fix = split(/,/,$mfx);
         $re_mfx  = join "|",@mid_fix;
         $re_mfx = qr/$re_mfx/;
}


if((length($nfx))>  0 and $nfx !~ /^\s+$/) {
        @not_fix = split(/,/,$nfx);
        $re_nfx   = join "|", @not_fix;
        $re_nfx      = qr/$re_nfx/;

It appears that nofix only occurs at the end of a string so:

        $re_nfx      = qr/$re_nfx\z/;


}
my @listed;

foreach my $fname(@fnames) {
    if($fname =~ /^.*?$re_nfx$/) {

The '^.*?' at the front just adds complexity that you don't need, only the end=of-string anchor is required:

     if ( $fname =~ /$re_nfx$/ ) {


        #print "FILE NAME [$fname] is not a valid file \n";
        next;
    }elsif ($fname =~ /^$re_pfx/){

Because $pfx contains the string '' the value of $re_pre will be undefined so when I run your code I get these messages:

Use of uninitialized value $re_pfx in regexp compilation at your_code.pl line 60.

You may want to test for defined() before trying to execute the regular expressions.


        push(@listed,$fname);
    }elsif ($fname =~ /^.*$re_sfx$/) {

The '^.*' at the front just adds complexity that you don't need, only the end=of-string anchor is required:

    } elsif ( $fname =~ /$re_sfx$/ ) {


        push(@listed,$fname);
    }elsif ($fname =~ /^.*$re_mfx.*$/) {

The '^.*' at the front and the '.*$' at the end just adds complexity that you don't need:

    } elsif ( $fname =~ /$re_mfx/ ) {


        push(@listed,$fname);
    }elsif() {

The empty conditional is a syntax error so this elsif block should be removed.

        push(@listed,$fname);
    }else {
        push(@listed,$fname);
    }
}

Each block after the initial if block executes the statement push(@listed,$fname); so your loop could be simplified to:

for my $fname ( @fnames ) {
    next if $fname =~ /$re_nfx$/;
    push @listed, $fname;
    }


for(@listed) {
        print $_,"\n";
}

prefix,suffix and midfix can be defined and can not be.
So in the above scenario file ABCD_1234.txt should not be selected
because it is not matching $sfx but I m not able to filter this.

So, in summery, this looks like it does what you require:

my ( $re_pfx, $re_sfx, $re_mfx, $re_nfx );

if ( defined $pfx && $pfx =~ /\S/ ) {
    my @pre_fix = map quotemeta, split /,/, $pfx;
    #find_largest_element( @pre_fix );
    $re_pfx = join '|', @pre_fix;
    $re_pfx = qr/\A$re_pfx/;
    }

if ( defined $sfx && $sfx =~ /\S/ ) {
    $re_sfx = join '|', map quotemeta, split /,/, $sfx;
    $re_sfx = qr/$re_sfx\z/;
    }

if ( defined $mfx && $mfx =~ /\S/ ) {
    $re_mfx = join '|', map quotemeta, split /,/, $mfx;
    $re_mfx = qr/$re_mfx/;
    }

if ( defined $nfx && $nfx =~ /\S/ ) {
    $re_nfx = join '|', map quotemeta, split /,/, $nfx;
    $re_nfx = qr/$re_nfx\z/;
    }

my @listed;

for my $fname ( @fnames ) {
    if ( defined $re_nfx && $fname =~ $re_nfx ) {
        #print "FILE NAME [$fname] is not a valid file \n";
        next;
        }
    elsif ( defined $re_pfx && $fname =~ $re_pfx ) {
        push @listed, $fname;
        }
    elsif ( defined $re_sfx && $fname =~ $re_sfx ) {
        push @listed, $fname;
        }
    elsif ( defined $re_mfx && $fname =~ $re_mfx ) {
        push @listed, $fname;
        }
    }

for ( @listed ) {
    print "$_\n";
    }




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to