Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Ignore file in array without looping

by Anonymous Monk
on Mar 27, 2013 at 18:05 UTC ( #1025776=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I need to check if some code letter is part of a file name, and if its not, ignore the file, having issues trying to do this without a loop since I am already in a "for" loop. If I have no choice I would use another loop.
Here is a sample code to show what I am trying to do:
... my @array_code = qw(x-01 x-02 x-03 x-04 v-05 v-06 v-07 x-08 x-09 x-10 +x-11); for my $file (read_dir( $dir_list, prefix => 1 )) { #files will be: 1234567_x-01_2013_03.txt 1234667_x-03_2013_03.txt + 0034567_y-01_2013_03.txt 1234567_v-01_2013_03.txt 1233567_v-05_2013_ +03.txt 1234567_x-55_2013_03.txt next if grep { /$file/ } @array_code; # not working next if(! -f $file || -s $file < $min_size) ; if(-s $file <= $max_size) { push @files, $file; }else { push @bigsize_files, $file; } } ...
Thanks for the help!

Comment on Ignore file in array without looping
Download Code
Re: Ignore file in array without looping
by hdb (Prior) on Mar 27, 2013 at 18:17 UTC

    My proposal: build a dynamic pattern from your @array_code

    my @array_code = qw(x-01 x-02 x-03 x-04 v-05 v-06 v-07 x-08 x-09 x-10 +x-11); my $pattern = "(".join("|", @array_code ).")"; my @files = qw(1234567_x-01_2013_03.txt 1234667_x-03_2013_03.txt 0034 +567_y-01_2013_03.txt 1234567_v-01_2013_03.txt 233567_v-05_2013_03.tx +t 1234567_x-55_2013_03.txt); for my $file (@files) { next if $file =~ /$pattern/; print $file,"\n"; }
Re: Ignore file in array without looping
by Corion (Pope) on Mar 27, 2013 at 18:18 UTC
    ... next if grep { /$file/ } @array_code; # not working

    How is that grep not working? Can you describe in English what that line is supposed to do?

    Maybe it helps you diagnose this if you print out the two values for each comparison?

    ... next if grep { warn "Checking if $_ =~ /$file/"; /$file/ } @array_code +; ...
Re: Ignore file in array without looping
by Neighbour (Friar) on Mar 28, 2013 at 09:08 UTC
    You probably want
    next if grep { $file =~ /$_/; } @array_code;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025776]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-11-24 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (135 votes), past polls