Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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!

Replies are listed 'Best First'.
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 hdb (Monsignor) 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 Neighbour (Friar) on Mar 28, 2013 at 09:08 UTC
    You probably want
    next if grep { $file =~ /$_/; } @array_code;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025776]
Approved by toolic
[holli]: You see, I have a friend whose daughter has been damaged seriously because she wasn't vaccinated against measles. (Her mother being anti-vaxx).
[LanX]: Contrary to Olivia newton John I don't like physical confrontations ...
[LanX]: Oh, you spread measles in your saliva?
[LanX]: ... and why did you bite hos daughter?
[LanX]: *his
[holli]: And this guy was also Anti-Vaxx and I just snapped. I shoed him all over the yard
[james28909]: biting between the legs? now thats my kind of religion.
[holli]: and back into the building, saw him later peeking out of the window looking scared. It was hilarious.
[LanX]: Let's get physical...
[james28909]: wish i could stay and chat. i have an unsuspecting client to give an estimate to. muhhahaha

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2017-12-13 18:07 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (373 votes). Check out past polls.