use strict and warnings. always ask perl to help you. this will save you
many hours of debug time later on.
J> open (MAIN, $file);
that is a very bad way to open a file. use lexical handles, 3 arg open
and check for failure and handle it.
open( my $main, '<', $file ) or die "can't open $file $!" ;
J> while (<MAIN>)
while ( my $line = <$main> ) {
is much better. no need to assign $_ later on.
J> {
J> chomp;
J> $count_rec++;
no need for that. the perl var $. is the record number. see perldoc perlvar.
J> if ($key eq "yes") {
J> open (LOCAT, $csv);
same thing. do a better open. what if it fails? you won't know, your
program continues to run or even just exit without any info.
J> $tble_loc = $_;
J> while (<LOCAT>) {
J> chomp;
see, you just clobbered your $_ (though you saved it). use lexical loop
variables and this will never be a worry
J> ($loc,$chkout,$renwal,$total) = split(/,/);
J> if ($loc eq $tble_loc) {
J> $total_first = $total;
J> print LOCAT ($_ . ",done\n");
you can't print to a file opened for only reading. and even if you did
open it for read/write, you usually can't print to it without a seek
call to reverse the i/o flow. and even IF you could do this, it would
clobber the next line you will read in (or even more if the write is
long). this is against all current designs of file systems (other than
some odd ones like vms which actually do true line oriented records).
the proper way is to read in this file line by line and write out a new
one printing the old lines and appending the new stuff as needed.
J> $count2++;
J> }
J> }
J> close (LOCAT);
if you did lexical handles, then that line isn't needed. the file will
be closed when the handle leaves scope.
J> }
J> }
J> close (MAIN);
J> </code>
J> What happens above is I open a file then while i loop through it, I
J> open another file, compare the codes in each file, and when I find it
J> the total from the record i the second file is added to an
J> accumulator. This is repeated for every code in the first file.
what you want is simple enough. your method will never work as
coded. you have to copy and modify the file on the fly. it can't be done
in place as lines are nothing more than newlines in the text
stream. think about what i said and you will see the error of your
design.
uri