Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read a line from a file and writing to the same line in the file 1

Status
Not open for further replies.

bcdixit

Technical User
Nov 11, 2005
64
US
How do I write to the same line in a file after reading from the that line.

#!/usr/contrib/bin/perl

$i = "c.txt";
$flag = 0;
if (-e $i )
{
open(RW,"+<", $i);
}
else
{
open(RW,"+>", $i);
print RW "0";
$flag = 1;
}

if($flag == 0)
{
while(<RW>)
{
$tmp = $_;
}
$tmp=$tmp+1;
print RW $tmp;
}
close RW;

I want this program to create a file called c.txt if it doesnot exist and on the first run of the perl script it should write a '0' to first line in the file. the next time I run the script, it should read the first line(i.e. a 0), increment it by 1 and then re-write to the same line. i.e. the first line on the file should be written with '1'.
Currently when i run the script for the first time, it correctly write a '0' on the first line but when I rerun the script it appends the incremented value i.e. a '1' (writes a '01') on the first line. I expect it to write a '1' on first line.
Any help is appreciated.

Thanks
 
use Tie::File ??

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
is there another way of doing this...
I am getting this ...
Can't locate TIE/File.pm in @INC (@INC contains: /opt/perl5/lib/5.6.1/PA-RISC2.0 /opt/perl5/lib/5.6.1 /opt/perl5/lib/site_perl/5.6
.1/PA-RISC2.0 /opt/perl5/lib/site_perl/5.6.1 /opt/perl5/lib/site_perl .)
 
still the same error after ishnids comments. I don't think this module is availalle on the box....and its a pain in the axx to get the sysadmin to compile it.
 
It's a pure Perl module so it doesn't need to be compiled. Have a look here for more on installing modules when you don't have root access.
 
Very old version of perl, 5.6.1, might not have Tie::File. You can use perls inplace editor which is what Millers link will discuss. It's much more efficient than Tie::File anyway from what I have onserved.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
onserved = observed

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I checked Millers Link.. sorry but can you help me understand what is the "inplace editor"?
 
Miller's link actually recommends Tie::File, as it's standard from 5.8.0.

Installing the Tie::File module only involves creating one directory, copying one file and adding one line to the top of your script.

1) Create a directory called (for example) /home/username/modules/Tie

2) Grab the source code from here and save it in a file called (following the example above) /home/username/modules/Tie/File.pm

3) Then in your script, before you load the module with "use Tie::File;", add the following line:
Code:
use lib '/home/username/modules';
 
using perl inplace editor:

Code:
[gray]#!/usr/contrib/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$i[/blue] = [red]"[/red][purple]c:/perl_test/c.txt[/purple][red]"[/red][red];[/red]
[olive][b]if[/b][/olive] [red]([/red] [url=http://perldoc.perl.org/functions/-X.html][black][b]-e[/b][/black][/url] [blue]$i[/blue] [red])[/red][red]{[/red]
   [url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url] [blue]@ARGV[/blue] = [red]([/red][blue]$i[/blue][red])[/red][red];[/red]
   [black][b]local[/b][/black] [blue]$^I[/blue] = [red]'[/red][purple].bak[/purple][red]'[/red][red];[/red]
   [olive][b]while[/b][/olive] [red]([/red] <> [red])[/red] [red]{[/red]
      [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] ++[blue]$_[/blue][red];[/red]
   [red]}[/red]
[red]}[/red]
[olive][b]else[/b][/olive] [red]{[/red]
  [url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]RW, [red]"[/red][purple]>[/purple][red]"[/red], [blue]$i[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
  [black][b]print[/b][/black] RW [red]"[/red][purple]0[/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
this part:

Code:
else {
  open(RW, ">", $i) or die "$!";
  print RW "0";
}

better written as:

Code:
else {
  open(RW, ">", $i) or die "$!";
  print RW "0";
  close RW; 
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top