I have two files - one which looks like this (FILE1)
1.10.10 1.10.1040 1.10.150 1.10.220
...etc

and another that looks like this (FILE2)

1.10.10.640 1.10.10.650 1.10.10.660 1.10.1040.20 1.10.150.290 1.10.150.300 1.10.150.310 1.10.220.80
...etc

I'm wanting to match the first three numbers from the 4 number strings in FILE2 (each number being separated by a '.') with the corresponding three number strings in FILE1 and then count how many matches there are.

For example for the three number string 1.10.10 in FILE1 there are 3 matches in FILE2.

I want the output file to look like this:

1.10.10 3 1.10.1040 1 1.10.150 3 1.10.220 1
I've got the following script
use strict; my $file1 = shift; my $file2 = shift; open(FILE1, $file1) or die "Cant open $file1:$!\n"; open(FILE2, $file2) or die "Cant open $file2:$!\n"; my @file1 =<FILE1>; my @file2 =<FILE2>; #print @file1; #print @file2; for(my $i=0; $i<@file1; $i++) { chop($file1[$i]); #print "F $file1[$i]"; for(my $j=0; $j<@file2; $j++) { chop($file2[$j]); my @array = split(/\./, $file2[$j]); my $cat = "$array[0]" . "." . "$array[1]" . "." . "$array[2]"; if("$file1[$i]" eq "$cat") { print "$file1[$i] $cat\n"; } } }
But its simply not working properly - its not finding all the matches. I've given only example files here - the actual files are much bigger - some matches are recognised but not all. Any pointers in the right direction much appreciated!!!

In reply to help requested with collating data from two files by Angharad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.