I am not brilliant at perl (pretty rubbish) and would like to compare the contents of two files preferably with an output file of all the entries which are the same and also a count of how many entries are the same.
So far I have a bit of code which takes two files and saves them to arrays - not sure if thats the best approach, my code so far is below but as yet doesn't get down to the important part as I haven't a scooby as how to achieve this (have tried a few things and they have not worked)
I want to be able to compare text files with one "tag" name per line
(have so far been using simple files: File1.txt - A,B,C,D,E,F,G,H AND File2.txt - I,J,F,A,B,K,C,Y,Z)
Expand|Select|Wrap|Line Numbers
- #!/usr/bin/perl;
- use warnings;
- use strict;
- #global variables for the program
- my $file1 = "";
- my $file2 = "";
- my $line1 = "";
- my @tag1;
- my $line2 = "";
- my @tag2;
- my $same = 0;
- my $tag1 = "";
- my $tag2 = "";
- #get user to input a file
- print "Please type in a file name for comparison, and press enter\n";
- #save file to $file1 variable
- $file1 = <STDIN>;
- #remove new line from input
- chomp $file1;
- #open file, if file does not exist end
- open (FILE1, "$file1") or die "Can't find file $file1";
- #save file into array @tag1
- while ($line1 = <FILE1>){
- #read in each line from file and copy to $line, each new line saved to array @names
- push (@tag1,$line1);
- }
- #Get user to enter an additional file to compare to
- print "Thank you\nPlease enter a second file for the comparison\n";
- #Save file to $file2
- $file2 = <STDIN>;
- #remove new line from input
- chomp $file2;
- #open file, if it does not exist end program
- open (FILE2, "$file2") or die "Can't find file $file2";
- #save file into array @tag2
- while ($line2 = <FILE2>){
- #read in each line from file and copy to $line, each new line saved to array @names
- push (@tag2,$line2);
- }