http://qs321.pair.com?node_id=1192908

hopper has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I need help in merging the difference between two files based on the same content within them and sorting and saving in the third file following are my example files:
File1: NAME, ID1, ID2 apple banana NAME, ID1, ID3 strawberry grape File 2: NAME, ID1, ID2 apple jackfruit NAME, ID1, ID4 banana grapes Desired output result: NAME, ID1, ID2 apple banana NAME, ID1, ID3 strawberry grape NAME, ID1, ID4 banana grapes However, this is what I get: NAME, ID1, ID2 NAME, ID1, ID3 NAME, ID1, ID4 #!/bin/perl -w use strict; use warnings; use File::Copy; use Cwd; my $dir = cwd; main(); sub main { printf "\nStarting script\n"; printf "\nEnter file 1: "; my $a = <STDIN>; chomp $a; printf "\n"; printf "Enter file 2: "; my $b = <STDIN>; chomp $b; my $output = "output.txt"; if(-e $a and -e $b) { my $counter = 0; my $counter2 = 0; my %results = (); open (FILEA, "<$a") or die "Input file $a not found.\n"; while(my $line = <FILEA>) { $counter++; if($line =~ /^NAME/) { my ($name, $variable1, $variable2) = split(',', $l +ine, 3); $results{$line}=1; } } close(FILEA); open (FILEB, "<$b") or die "Input file $b not found.\n"; while(my $line =<FILEB>) { if($line =~ /^NAME/) { my ($name, $variable1, $variable2) = split(',', $l +ine, 3); $results{$line}++; } } close(FILEB); open (OUTPUT, ">$output") or die "Cannot open $output for +writing \n"; foreach my $line (keys %results) { print OUTPUT "$line"; $counter = $counter if $counter != $counter2; } close OUTPUT; } }
I am new to perl so please someone could help me and direct me to the right direction.

Thanks so much in advance.