Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Filtering Source Text File with 2nd Text File of Terms

by Riales (Hermit)
on Apr 02, 2012 at 23:49 UTC ( #963150=note: print w/ replies, xml ) Need Help??


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; }


Comment on Re: Filtering Source Text File with 2nd Text File of Terms
Select or Download Code
Re^2: Filtering Source Text File with 2nd Text File of Terms
by jwkrahn (Monsignor) 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-09-19 03:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls