Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Writing to a file more efficently.

Posted on 1998-06-02
7
Medium Priority
?
232 Views
Last Modified: 2010-03-04
Hi,
    I'm writing to a file but find the method I'm using is in-efficent.  What I do is read in a file, store it in a array, modify the corressponding array element to the line number and then print everything back to the file.  Is there a simplier way to just write a single line to a file?  So if I wanted to just write line 2 over in a 5 line file, I wouldn't have to touch any other lines.

Thanks in Advance.
0
Comment
Question by:cide
7 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 1207038
perl -i.old -pe '$_ = "new line 2\n" if $.==2' file
0
 

Author Comment

by:cide
ID: 1207039
So do I actually use this line in the perl script?
0
 

Expert Comment

by:rkulper
ID: 1207040
Since the -i option rewrites the entire file anyway, I do not think it is any more efficient than what your currently doing. See perldoc perlrun -i option.

You could use the tell and seek functions, but this only works efficiently if all the line lengths are equal. If the line lengths are not equal, the only optimization I can think of is to use tell and seek to avoid changing the first unused part of the file.

Here is an example program that sorts the file "passwd" (assuming passwd contains lines of equal length).

#!/usr/local/bin/perl
$index=1; $position[0]=0;
unless (open FILE, "+<passwd") {
            print STDERR "Unable to open file: $!\n";
            exit;
}
while (<FILE>) {
      $array[$index]=$_;
      $position[$index]=tell FILE;
      $index++;
}
@sorted=sort @array;
$index=0;
while ($index <= $#array) {
      if($array[$index] ne $sorted[$index]) {
            seek FILE, $position[$index], 0;
            print FILE $sorted[$index];
      }
      $index++;
}
close FILE;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Expert Comment

by:ozo
ID: 1207041
It is more efficient in that it does not store the entire file in n array.  see
perldoc perlfaq5

("passwd" may be a poor example of a file assumed to contain lines of equal length)
Also, starting at $index = 1 may make the ne between @lines and @sorted work differently than you intended.
I'd have just used push and shift, and not bother with $index at all.
0
 

Author Comment

by:cide
ID: 1207042
What is this -i option everyone is talking about?
0
 
LVL 85

Expert Comment

by:ozo
ID: 1207043
see
perldoc perlrun
Within a script, you could also use

{local $^I=".old"; local @ARGV=("file.name");
while( <> ){
  $_ = "new line 2\n" if 2..2;
  print;
}
}

0
 
LVL 2

Accepted Solution

by:
tpryor earned 100 total points
ID: 1207044
i would suggest using the "tie" keyword in perl 5.  You can tie a hash or an array to a text file.  When you write to the array/hash it writes to that line in the text file.

for example if you use TieFile.pm you could say...

tie @lines, TieFile', 'foo.txt';
print $lines[20];

When asked to fetch the nth line the TieFile module reads the file until it reaches that line and returns it.  Since it is wasteful to keep traversing the entire file every time a line is requested, TieFile keeps track of the file offsets of the beginning of each line so that when you ask it for a line that it has already visited, it knows the precise offset to seek before reading.

so the file is not stored in an array in memory and you can deal with the file on a specific line by line basis.

is this  what your looking for?

let me know I'll paste the TileFile.pm here.

GL
t
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question