in reply to replace all but first

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;

Replies are listed 'Best First'.
Non-re solution
by Rhys (Pilgrim) on Sep 04, 2004 at 13:33 UTC
    Not bad, but to keep it a little more re-usable and flexible (suppose, for instance, more tags/values are added later), you can do away with the $company and $country variables altogether, and do:

    ... @myarray = split /></,$line; if ( $prev ne $myarray[0] ) { $prev = $myarray[0]; } else { $myarray[0] = "<TAB"; } $line = join '><', @myarray; print OUTFILE, $line; ...
    This also takes care of the fact that I think the original author meant <TAB> literally. If I'm wrong, insert this after the join:

    $line =~ s/<TAB>/\t/;