Ahmed Ahmed - 2 months ago 7
Perl Question

Compare all lines in a file

I have a file contains this text:

perl java python php scala
java pascal perl ruby ada
ASP awk php java perl
C# ada python java scala


I would like to compare each two line and display the number of shared word, I wrote a program who display the shared number of word between the first line and the others lines, first line and second line contain 2 (perl, java) common word, first and third have 3 words, and first and fourth have 3 words.
But I would like to compare also the second line with first, the third and the fourth, the same for the third line, and so on.
This is my code:

open(F,"/home/Bureau/test2/terms.txt")||die " Pb pour ouvrir";
my $firstLine = <F>;

my @a =split(/ /, $firstLine);
while (<F>) {

my @b = split(/ /,$_ );

my @intersection =
grep { defined }
@{ { map { lc ,=> $_ } @a } }
{ map { lc } @b };
print $#intersection."\n";
}


I think that the problem is how to change the contents of @a with the next line, I tried but I didn't arrive at a solution,
Thanks for all.

Answer

This solution builds an array of hashes corresponding to the lines of the array. Then two nested loops use grep to count the number of words that occur both in any given hash and all following ones

use strict;
use warnings 'all';

my @data;

while ( <DATA> ) {
    push @data, { map { $_ => 1 } split };
}

for my $i ( 0 .. $#data-1 ) {

    for my $j ( $i + 1 .. $#data ) {

        my $join = grep { $data[$i]{$_} } keys %{ $data[$j] };

        printf "Line %s has %d word%s in common with line %d\n",
            $i + 1,
            $join,
            $join == 1 ? '' : 's',
            $j + 1;        }
}



__DATA__
perl java python php scala
java pascal perl ruby ada 
ASP awk php java perl
C# ada python java scala

output

Line 1 has 2 words in common with line 2
Line 1 has 3 words in common with line 3
Line 1 has 3 words in common with line 4
Line 2 has 2 words in common with line 3
Line 2 has 2 words in common with line 4
Line 3 has 1 word in common with line 4
Comments