CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [ewh006] File Comparison (2 files): Matching Occurences: Edit Log



BillKSmith
Veteran

Feb 25, 2018, 8:52 PM


Views: 9293
Re: [ewh006] File Comparison (2 files): Matching Occurences

There are two possible approaches (with several variations of each). In the first, you search the email for each 'word' in the text file. In the second, you search the text file for each word in the email. I cannot recommend either one without knowing more about your problem.

  • How many words are in your text file? Are you concerned about processing speed?

  • How do you define 'word' in the email? Is it any contiguous string of characters which match an entry in the file? Is it a string of characters surrounded by whitespace? Anything else? Must we be concerned with case? word-wrap? plurals? tense? ...

  • Do you plan to extract the text of the email before you start?

  • Does your email contain non-ASCII characters?



  • UPDATE:

    Chris has provided an example of the first method. Here is an example of the second:


    Code
    C:\Users\Bill\forums\guru>type ewh006.pl 
    use strict;
    use warnings;

    my $email = \do{ my $mail = << 'END_EMAIL' };
    Here is some
    arbitrary text. It
    really does not matter much
    what it says -its just email.
    END_EMAIL

    my $text = \do{ my $word_list = << 'END_TEXT' };
    some
    text
    fum
    email
    foo
    END_TEXT


    open my $TEXT, '<', $text or die "Cannot open text file:$!";
    my @words = <$TEXT>;
    chomp @words;
    my $re = join '|', @words;
    my $regex = qr/$re/;


    open my $EMAIL, '<', $email or die "Cannot open email:$!";
    my $body = do{ local $/ = undef; <$EMAIL>};

    my @matched_words = $body =~ /($regex)/g;

    do{local $, = "\n"; print @matched_words;};


    C:\Users\Bill\forums\guru>perl ewh006.pl
    some
    text
    email
    C:\Users\Bill\forums\guru>

    Good Luck,
    Bill

    (This post was edited by BillKSmith on Feb 26, 2018, 12:06 PM)


    Edit Log:
    Post edited by BillKSmith (Veteran) on Feb 26, 2018, 7:07 AM
    Post edited by BillKSmith (Veteran) on Feb 26, 2018, 12:06 PM


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives