Re: compare 2 data files and extract fields for matched lines
- From: shree <srigowrisn@xxxxxxxxxxx>
- Date: Sat, 29 Dec 2007 13:45:53 -0800 (PST)
#!/usr/bin/perl
use warnings;
use strict;
my $File_In1 = 'dat1.txt';
my $File_In2 = 'dat2.txt';
open FILE_IN2, '<', $File_In2 or die "cannot open '$File_In2' $!";
my %population;
while ( <FILE_IN2> ) {
my ( $county, $state, $pop ) = /\A([^|]+)\|([^|]+)\|(\d+)\Z/;
$population{ "$state|$county" } = $pop;
}
close FILE_IN2;
open FILE_IN1, '<', $File_In1 or die "cannot open '$File_In1' $!";
while ( my $line = <FILE_IN1> ) {
chomp $line;
my ( $key ) = $line =~ /\|([^|]+\|[^|]+)\z/;
print "$line|", $population{ $key } || '0000', "\n";
}
close FILE_IN1;
__END__
John
Dear all,
Thanks for showing me how to do this. And an added thanks to John for
teaching good programming techniques in perl.
I was able to literally use the above and it worked like a charm.
Shree
.
- References:
- compare 2 data files and extract fields for matched lines
- From: shree
- Re: compare 2 data files and extract fields for matched lines
- From: John W. Krahn
- compare 2 data files and extract fields for matched lines
- Prev by Date: Re: PERL CGI Script Responding to Link
- Next by Date: Perl script to return the number of occurences of multiple lines in a file
- Previous by thread: Re: compare 2 data files and extract fields for matched lines
- Next by thread: FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong?
- Index(es):
Relevant Pages
|