in reply to Re: need help with a bug
in thread Unexpected output from while loop (was: need help with a bug)

#!/usr/local/bin/perl -w use strict; open(F1,"file1.txt") || die $!; open(F2,"file2.txt") || die $!; open(F3,">temp.txt") || die $!; while(1) { my $line1 = <F1>; chomp($line1); my ($name1,$phone1,$check1) = split(/\|/,$line1); my $line2 = <F2>; chomp($line2); my ($name2,$phone2,$check2) = split(/\|/,$line2); print F3 "$phone1,$name1,$check1,$check2," . ($check1-check2) . ',' +. ($check2 > $check1 ? 'decrease' : 'increase') . "\n"; last if eof; } close (F1); close (F2); close (F3);
results in:
523-3452,joe,300,200,100,increase 624-3850,mike,500,400,100,increase 327-3958,john,100,200,-100,decrease
Not very robust. Fails gracelessly if F2 is longer than F1. If the files will be short, consider slurping 'em both into arrays. If you're guaranteed unique combinations of names and phone numbers, consider using them as the keys to a hash whose values are a list or hashref of the check values, so as to remove dependency on the lists being in the same order.