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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading and writing simultaneously to a file

Status
Not open for further replies.

hbkid

Programmer
Mar 7, 2005
3
US
I'm trying to read and write to a file at the same time.
I used open ( FILE, +<$file_name) to make $file_name both readable and writeable.
I basically want to replace
x
y
z

in the file
with
x
a
y
z

When I tried doing this with an algorithm that works like this

if $line = /x/
print FILE a;
This did not work
How do I do this?.
Thanks in advance,
 
thats not an algorithm, nor is it a regexp, its an assingment operator (=) which is not what you want to do. This is probably what you want to do:

Code:
while (<FILE>) {
   print FILE "x\na\n" if (/x/);
}

or more like you were trying:

Code:
while (<FILE>) {
   $line = $_;   
   if ($line [b]=~[/b] /x/) {
      print FILE "x\n";
      print FILE "a\n";
   }
}

although I am not sure how well supported the "+<" operator is.
 
Is there a reason you couldn't use a temp file? I didn't test the code, but something like:

Code:
open IN, "< $file_name" or die;
open OUT, "> ${file_name}.tmp" or die;

while ($line = <IN>) {
    print OUT $line;
    if ($line =~ /x/) {
        print OUT "a\n";
    }
}
close IN;
close OUT;

unlink($file_name);
rename("${file_name}.tmp", $file_name);
 
When you write to a file in that way, you are overwriting the current contents, not inserting.

You would be better off either making a copy of the file, then reading in the copy and writing to the original, or reading the entire file into an array and writing out the new version.
 
won't this overwrite the line with x in it instead of insert a after x?

Code:
while ($line = <IN>) {
    print OUT $line;
    if ($line =~ /x/) {
        print OUT "a\n";
    }
}
 
Sorry, I may have over simplified the problem..
What I do have is a hash and I need to install the 2 values in the hash after line x depending on the if condition below. The code I have so far.


open (OUT, "+<$out_file") || die "Cannot open output.txt";
while (defined ($line = <OUT>)) {
foreach $key (sort keys %cell_port_hash) {
@key_array = split ('_', $key);
$key1 = $key_array[0];
if (defined $key1) {
if ($line =~ /$key1/) {
if ($cell_port_hash{$key} !~ /O/) {
print OUT "\(\"$cell_port_hash{$key}\" \"Input\" \)\n";
} else {
print OUT "\(\"$cell_port_hash{$key}\" \"Output\" \)\n";
}
}
}
}
}
 
KevinADC, I decided to test the code since there was some uncertainty.

Code:
while ($line = <DATA>) {
    print $line;
    if ($line =~ /x/) {
        print "a\n";
    }
}

__DATA__
x
y
z

Output:
Code:
x
a
y
z
 
I like to use Tie::File for problems like this:
Code:
#!/usr/bin/perl -w
use strict;

use Tie::File;

tie my @file_array, 'Tie::File', 'test.dat';

my $i = 0;
while ( $i < @file_array ) {
   if ( $file_array[ $i ] =~ /x/ ) {
      splice @file_array, ++$i, 0, 'a';
   }
   $i++;
}
untie @file_array;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top