Hi,
I have two text files.
The first one has a list of words , like the following :
File 1.txt

Laura
Samuel
Gerry
Peter
Maggie

The second one has paragraphs on it. For e.g. :
File2.txt

Laura
is
about
to
meet
Gerry
and
is
planning
to
take
Peter
along

All is want the program to do is , look for common words and print "Match" beside the matching words in File2.txt or to a third output file.
So the desired output should look like this.

    Laura | MATCH
    is
    about
    to
    meet
    Gerry | MATCH
    and
    is
    planning
    to
    take
    Peter | MATCH
    along

I have tried the following code , however I am not getting the desired output. Please help

use warnings;
use strict;
use Data::Dumper;
my $result = {};

my $first_file  = shift || 'File1.txt';
my $second_file = shift || 'File2.txt';
my $output = 'output2.txt';

open my $a_fh, '<', $first_file  or die "$first_file: $!";
open my $b_fh, '<', $second_file or die "$second_file: $!";
open (OUTPUT,'>'.$output) or die "Cannot create $output.\n";

while(my $line = <$a_fh>) {
    chomp;
    next if /^$/;
    $result->{$_}++;    
}
while(my $line = <$b_fh>) {
    chomp;
    next if /^$/;
    if ($result->{$_}) {
        delete $result->{$_};
    $result->{join " |"=> $_,"MATCH"}++;
    }
    else {
        $result->{$_}++;
    }
}
{   $Data::Dumper::Sortkeys = 0;
    print OUTPUT Dumper $result;
}

However , the output that I am getting is like this.

   Laura  | MATCH
    Samuel | MATCH
    take
    Maggie | MATCH
    Laura
    about
    to
    Gerry
    meet
    Gerry | MATCH
    and
    is
    Maggie |MATCH
    planning
    to
    Peter |MATCH
    take
    Peter |MATCH
  • the output is not in a paragraph format , neither its printing MATCH for all matches.
    Please advise.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.