Here is my solution. It is no where as elegant and short as the previous ones. Comments on where I can improve this code and what practices I should stay away from are appreciated.
use strict;
#OPEN FILE A.txt for READING (CHECK FOR FAILURES)
open (INFILE, "<","A.txt" ) or die "Could not open file A.txt: $!";
#OPEN FILE B.txt for WRITING (CHECK FOR FAILURES)
open (OUTFILE, ">","B.txt" ) or die "Could not open file B.txt: $!";
my $prev_country = "";
my $line;
my @myarray;
my $country;
my $company;
while ($line = <INFILE>) {
# SPLIT THE FILE BETWEEN COUNTRY AND COMPANY
@myarray = split /></,$line;
$country = $myarray[0];
# CHECK TO SEE IF THE CURRENT COUNTRY MATCHS THE PREVIOUS COUNTRY
if ($prev_country ne $country) {
#IF NEW COUNTRY PRINT WHOLE LINE
print OUTFILE "$line";
$prev_country = ($country);
next;
}
# IF SAME COUNTRY PRINT ONLY COMPANY
$company = $myarray[1];
print OUTFILE "\t<$company";
}
close OUTFILE;
close INFILE;