http://www.perlmonks.org?node_id=963150


in reply to Filtering Source Text File with 2nd Text File of Terms

Your main problem is when you check to see if the source matches any of the terms, you're only checking the last term in the file.

You're also trying to print a match with the $1 but that's not really what you want.

Beyond that, is there a particular reason you're choosing to use hashes instead of arrays? I would think arrays are more what you want.

# Building the array of terms: my @terms = (); while (my $term = <F1>) { chomp $term; push @terms, $term; }

This way, when you are checking each term against the source, you just need to do this:

# Printing sources that do not match of of the terms: while (my $source = <F2>) { chomp $source; print "$source\n" unless grep { $source =~ /$_/ } @terms; }

Replies are listed 'Best First'.
Re^2: Filtering Source Text File with 2nd Text File of Terms
by jwkrahn (Abbot) on Apr 03, 2012 at 05:25 UTC
    while my $term (<F1>) {

    That is a syntax error.    Perhaps you meant:

    while ( my $term = <F1> ) {


    foreach my $source (<F2>) {

    Why would you read in the whole file instead of just reading one line at a time?    Perhaps you meant:

    while ( my $source = <F2> ) {

      Argh, you're absolutely right. I guess I was just too eager to fire off my response. I'll change my original post.

      Thanks for catching that.
Re^2: Filtering Source Text File with 2nd Text File of Terms
by Loops303 (Novice) on Apr 04, 2012 at 04:33 UTC
    i think i was considering a hash would allow me to check all the terms at once, as opposed to do it a line at a time and output unfiltered items into the output... but thanks for the tip, i will give it a try. very helpful. this site rules.