purge line

G

George Mpouras

I want my line to be 5 not 5111



my $counter;
open FILE, '+<:raw', 'test.txt' or die "$!\n";

seek FILE, 0, 0;
read ( FILE , $counter , 64 );
print $counter;

seek FILE, 0, 0; print FILE 1111;
seek FILE, 0, 0; print FILE 5;

close FILE;
 
G

George Mpouras

# this seems to work but probably there is something better

my $counter;
open FILE, '+<:raw', 'test.txt' or die "$^E\n";

seek FILE, 0, 0;
read ( FILE , $counter , 70 );
$counter=~tr/\x20//sd;

print "*$counter*";

$counter = 1111; seek FILE, 0, 0; printf FILE "%64d", $counter;
$counter = 5 ; seek FILE, 0, 0; printf FILE "%64d", $counter;
close FILE;
 
P

Peter Makholm

George Mpouras said:
seek FILE, 0, 0; print FILE 1111;
seek FILE, 0, 0; print FILE 5;

If you need to truncate the file you have to do it explicitely. The
function you are looking for is called 'truncate'.

//Makholm
 
G

George Mpouras

Στις 14/1/2014 11:35, ο/η Peter Makholm έγÏαψε:
If you need to truncate the file you have to do it explicitely. The
function you are looking for is called 'truncate'.

//Makholm


not truncate , but read/update
 
P

Peter Makholm

George Mpouras said:
not truncate , but read/update

What happens if you add a 'truncate FILE, 0;' right before printing the
second number?

For me it seems to do as you require. Of course this assumes that you
only have a single value in the file and don't need to update a value in
the middle of a larger file.

If you have multiple values in the file and need to update a single
value you would either need to rewrite all values following the value
you need to change og used a fixed format where the values have a fixed
length.

//Makholm
 
G

George Mpouras

Στις 14/1/2014 13:32, ο/η Peter Makholm έγÏαψε:
What happens if you add a 'truncate FILE, 0;' right before printing the
second number?

For me it seems to do as you require. Of course this assumes that you
only have a single value in the file and don't need to update a value in
the middle of a larger file.

If you have multiple values in the file and need to update a single
value you would either need to rewrite all values following the value
you need to change og used a fixed format where the values have a fixed
length.

//Makholm


i tried , do not work seem to work at multiple iterations
 
R

Rainer Weikusat

George Mpouras said:
Στις 14/1/2014 13:32, ο/η Peter Makholm έγÏαψε:
What happens if you add a 'truncate FILE, 0;' right before printing the
second number?
[...]

i tried , do not work seem to work at multiple iterations

--------
open($fh, '>', '/tmp/out') or die("$!");

while ($data = <STDIN>) {
seek($fh, 0, 0);
truncate($fh, 0);
print $fh ($data);

system('cat', '/tmp/out');
}
 
G

George Mpouras

Στις 14/1/2014 16:46, ο/η Rainer Weikusat έγÏαψε:
open($fh, '>', '/tmp/out') or die("$!");

while ($data = <STDIN>) {
seek($fh, 0, 0);
truncate($fh, 0);
print $fh ($data);

system('cat', '/tmp/out');
}



this works but for reading the counter you need the shell command

system('cat', '/tmp/out');
 
R

Rainer Weikusat

George Mpouras said:
Στις 14/1/2014 16:46, ο/η Rainer Weikusat έγÏαψε:



this works but for reading the counter you need the shell command

system('cat', '/tmp/out');

Not really. That's just what I used because it was the easiest way to
display content of the file.
 
G

George Mpouras

Στις 14/1/2014 17:17, ο/η Rainer Weikusat έγÏαψε:
Not really. That's just what I used because it was the easiest way to
display content of the file.


really
 
R

Rainer Weikusat

George Mpouras said:
Στις 14/1/2014 17:17, ο/η Rainer Weikusat έγÏαψε:
really

------
open($fh, '+>', '/tmp/out') or die("$!");

while ($data = <STDIN>) {
seek($fh, 0, 0);
truncate($fh, 0);
print $fh ($data);

print("content of out\n");
seek($fh, 0, 0);
printf("\t%s", $_) for <$fh>;
}
------
 
G

George Mpouras

Στις 14/1/2014 18:02, ο/η Rainer Weikusat έγÏαψε:
open($fh, '+>', '/tmp/out') or die("$!");

while ($data = <STDIN>) {
seek($fh, 0, 0);
truncate($fh, 0);
print $fh ($data);

print("content of out\n");
seek($fh, 0, 0);
printf("\t%s", $_) for <$fh>;
}
------

$fh can not read at start the stored value ...
 
R

Rainer Weikusat

George Mpouras said:
Στις 14/1/2014 18:02, ο/η Rainer Weikusat έγÏαψε:

$fh can not read at start the stored value ...

There is no 'stored value at start' in this example because creating the
file with +> truncates it to zero.
 
G

George Mpouras

Στις 14/1/2014 19:46, ο/η Rainer Weikusat έγÏαψε:
There is no 'stored value at start' in this example because creating the
file with +> truncates it to zero.

I know, that is why I end up at the solution without the truncate
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top