Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: replace all but first

by terra incognita (Pilgrim)
on Sep 03, 2004 at 16:46 UTC ( [id://388341]=note: print w/replies, xml ) Need Help??


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/;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://388341]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found