Writing to the end of a record

J

Joe

Hi,

I am trying to write to the end of a record once I find the data
needed, but everything I have tried and the Internet is no real help.

I open my data file by using 'open (LOCATE +<$filename') and the data
is processed correctly when a match is found. It is when I try to
write to the end is when I have trouble.

I have tried, using 'print LOCATE ($_ . ", done")', but when I run the
program the data is never written to the file

If someone could tell me what i am doing wrong, I would really
appreciate it.
 
U

Uri Guttman

J> Hi,
J> I am trying to write to the end of a record once I find the data
J> needed, but everything I have tried and the Internet is no real help.

J> I open my data file by using 'open (LOCATE +<$filename') and the data
J> is processed correctly when a match is found. It is when I try to
J> write to the end is when I have trouble.

hopefully that isn't the full open call as it won't compile. please show
real code when you post and not something you typed here.

J> I have tried, using 'print LOCATE ($_ . ", done")', but when I run the
J> program the data is never written to the file

try doing a seek between the read and write. see perldoc -f seek for why
and how to do that.

uri
 
P

Permostat

Hi,

I am trying to write to the end of a record once I find the data
needed, but everything I have tried and the Internet is no real help.

I open my data file by using 'open (LOCATE +<$filename') and the data
is processed correctly when a match is found.  It is when I try to
write to the end is when I have trouble.

I have tried, using 'print LOCATE ($_ . ", done")', but when I run the
program the data is never written to the file

 If someone could tell me what i am doing wrong, I would really
appreciate it.

Blast it with piss.

Herm=
 
J

Joe

Blast it with piss.

Herm=
<code>
Here is the ccde i am using:
open (MAIN, $file);
while (<MAIN>)
{
chomp;
$count_rec++;

if ($key eq "yes") {
open (LOCAT, $csv);

$tble_loc = $_;

while (<LOCAT>) {
chomp;
($loc,$chkout,$renwal,$total) = split(/,/);

if ($loc eq $tble_loc) {
$total_first = $total;
print LOCAT ($_ . ",done\n");
$count2++;
}
}
close (LOCAT);
}
}
close (MAIN);
</code>

What happens above is I open a file then while i loop through it, I
open another file, compare the codes in each file, and when I find it
the total from the record i the second file is added to an
accumulator. This is repeated for every code in the first file.

I hope this explains better what I am trying to do.
 
U

Uri Guttman

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top