CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [Tejas] Merging the data in two files using a hash: Edit Log



Laurent_R
Veteran / Moderator

Sep 4, 2014, 2:36 PM


Views: 41532
Re: [Tejas] Merging the data in two files using a hash

If you need a more detailed output than what I suggested above, you might try the following.

This assumes that 6 filehandlers are open before we start:
- $IN1 and $IN2 for the two input files
- $ORPH1 and $ORPH2 for orphans (records in one file but not in the other)
- $OUT1 and $OUT2 for common lines (two files because the keys of the input files might be the same and the content not necessarily be exactly identical)
Of course, you can simplify all this if some files are not needed.

The comparison function needs to be slightly different than before, because it needs to return three possible values:



Code
sub compare2 {  
my ($curr_key, $prev_key) = (split /,/, $_)[0] for @_;
return $curr_key cmp $prev_key;
}


Now the actual comparison code:

Code
my $ligne1 = <$IN1>; 
my $ligne2 = <$IN2>;
die "One of the input files is empty\n" unless defined $ligne1 and defined $ligne2;
chomp ($ligne1, $ligne2);
while ( 1 ) {
my $comparison = compare2($ligne1, $ligne2);
if ($comparison > 0) {
print $ORPH2 $ligne2, "\n";
$ligne2 = <$IN2>;
last unless defined $ligne2;
chomp $ligne2;
} else {
if ($comparison < 0) {
print $ORPH1 $ligne1, "\n";
$ligne1 = <$IN1>;
last unless defined $ligne1;
chomp $ligne1;
} else {
print $OUT1 $ligne1, "\n";
print $OUT2 $ligne2, "\n";
$ligne1 = <$IN1>;
$ligne2 = <$IN2>;
last unless defined $ligne1 and defined $ligne2;
chomp ($ligne1, $ligne2);
}
}
}
print $ORPH2 $ligne2 if defined $ligne2;
print $ORPH2 $ligne2 while $ligne2 = <$IN2>;
print $ORPH1 $ligne1 if defined $ligne1;
print $ORPH1 $ligne1 while $ligne1 = <$IN1>;

Same comment as my previous post: I haven't tested on your data, because I don't have enough of your data, so I might have goofed one detail here or there, but my module from which I took the code has been thoroughly tested in real life applications and is believed to be bug free.


(This post was edited by Laurent_R on Sep 4, 2014, 2:42 PM)


Edit Log:
Post edited by Laurent_R (Veteran) on Sep 4, 2014, 2:42 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives