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



Zhris
Enthusiast

Feb 26, 2018, 9:38 PM


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

Hi,

You're welcome.

I see from your screenshot you have each line from your keywords file in the @data array. Your array will contain data you don't need, your next step is to extract what you need. You don't need each case variation of each word, it is probably suitable just to later use the i modifier on a regular expression to match words in every variation of case.


Code
use strict; 
use warnings;
use Cwd;
use Data::Dumper;

my $filepath_keywords = getcwd . '/keywordsb.txt';

open my $filehandle_keywords, '<', $filepath_keywords or die "cannot open '$filepath_keywords': $!";
my @keywords = map { /\)\s*(\w+)/ } <$filehandle_keywords>;
close $filehandle_keywords;

print Dumper \@keywords;


This is very similar to what you have done so far with a couple of improvements.
- It uses the recommended three way form of open, with lexical scoped filehandle variable and error statement.
- It filters each line with map to extract what you need ( the first word that follows ") " ).

Check screenshot for output.

I suggest you read through Bill's solution carefully and understand what it is doing at each step, it is a general solution to your overall task, further requirements can easily be fitted in. Consider each of his questions too, they are important in deciding which approach is best for your needs, including any necessary fine tuning to make the code production ready.

The five steps you provided are the right train of thought. Reading the entire email in ( slurping ) is easier to process in your case, particularly on such small files. You'll find splitting won't be as accurate as using a regular expression, take for example words that end in a fullstop, this isn't even a necessary step with Bill's solution. Perl has an excellent loop system that avoids the need for explicit counters to iterate over data in typical cases.

There are of course solutions that incorporate grep and/or awk, though Perl is just as capable, this is a great little task to help you develop your Perl skills.

Regards,

Chris


(This post was edited by Zhris on Feb 26, 2018, 10:04 PM)
Attachments: output.png (67.5 KB)


Edit Log:
Post edited by Zhris (Enthusiast) on Feb 26, 2018, 10:04 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