wrote:
> Thanks to help from this newsgroup I've learned how to put together
> simple Perl scripts. Perl has saved me tons of time in my work and
> I've enjoyed learning to use Perl.
>
> Now I have a more challenging problem that I'm not experienced enough
> to
> figure out myself. I need to modify a file with data from a second
> file.
> Certain rows in the first file need to be replaced by the corresponding
> rows in the second file. Here are the details:
>
>
> File 1, filename.aro
> An input file to a commercial flow network code with the following
> format. The file size is relatively small, maybe 300 lines, ~100 kb:
> [Header]
> Header data
>
> [Preferences]
> Preference data
>
> [Junctions]
> 4,1,...comma delimited data
> 4,504,...comma delimited data
> 12,3,...comma delimited data
> 3,4,...comma delimited data
>
> [Pipes]
> 1,Pipe,...comma delimited data
> 605,Pipe,...comma delimited data
> 3,Pipe,...comma delimited data
> 10,Pipe,...comma delimited data
> 11,Pipe,...comma delimited data
>
> Certain rows in the [Junctions] and [Pipes] sections need to be
> replaced
> with corresponding lines in file 2. The rows to be replaced will have
> matching data in the first two comma delimited fields. This file will
> be
> very small, typically about 20 lines.
>
>
> File 2, filename.csv:
> 4,504,...new comma delimited data
> 3,4,...new comma delimited data
> 605,Pipe,...new comma delimited data
> 3,Pipe,...new comma delimited data
>
>
> In this example, the 4 lines in file 2 need to overwrite the
> corresponding lines in file 1. I've put together the following script
> but I can't get the comparison of the first two comma delimited
> fields to work. I haven't been able to figure it out and any guidance
> would be appreciated.
>
> Thanks, Dave
>
> #!C:\Perl\bin\perl.exe
> # aftmod.pl aftfilename.aro
>
> # Update AFT Branch and Pipe Data from Comma Delimited Text File (.csv)
> # The directory must contain a comma delimited file with the same
> # filename that defines the modifications: aftfilename.csv
>
> use warnings;
> use strict;
>
> # Get AFT model name from command line argument.
> my $filename=$ARGV[0];
> $filename =~ s/\.[^.]*$//; # Strip extension
>
> #Copy .aro to OLD.aro
> system "copy $filename.aro $filename-OLD.aro\n";
>
> # Open files for processing
> open (OUT, ">$filename.aro") or die;
> open (CSV , "<$filename.csv") or die;
> open (OLD , "<$filename-OLD.aro") or die;
>
> # Store CSV file into array
> my @csv_lines = <CSV>;
>
> # Process OLD .aro file
> foreach my $aro_line (<OLD>) {
> if ($aro_line !~ /,/) {
> print OUT ($aro_line); # Output non-comma delimited rows
> }
> else {
> # Seriously f'd up here
> foreach my $csv_line (@csv_lines) {
> if ($aro_line =~ /^((\w+),(\w+),)/ and $csv_line =~
> m{/$1$}i) {
> print OUT ($csv_line);
> }
> else {
> print OUT ($aro_line);
> last;
> }
> }
> }
> }
>
> # Close Files
> close OLD;
> close CSV;
> close OUT;
Here is one way to do it (UNTESTED):
#!C:\Perl\bin\perl.exe
# aftmod.pl aftfilename.aro
# Update AFT Branch and Pipe Data from Comma Delimited Text File (.csv)
# The directory must contain a comma delimited file with the same
# filename that defines the modifications: aftfilename.csv
use warnings;
use strict;
# Get AFT model name from command line argument.
( my $filename = $ARGV[ 0 ] ) =~ s/\.[^.]*\z//; # Strip extension
# rename .aro to OLD.aro
rename "$filename.aro", "$filename-OLD.aro"
or die "Cannot rename $filename.aro to $filename-OLD.aro: $!";
# Open files for processing
open CSV, '<', "$filename.csv" or die "Cannot open $filename.csv: $!";
open OLD, '<', "$filename-OLD.aro" or die "Cannot open $filename-OLD.aro: $!";
open OUT, '>', "$filename.aro" or die "Cannot open $filename.aro: $!";
my %csv;
while ( <CSV> ) {
my $key = join ',', ( split /,/ )[ 0, 1 ];
$csv{ $key } = $_;
}
close CSV;
while ( <OLD> ) {
my $key = join ',', ( split /,/ )[ 0, 1 ];
$_ = $csv{ $key } if exists $csv{ $key };
print OUT;
}
close OUT;
close OLD;
__END__
John
--
use Perl;
program
fulfillment