473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how 2 edit file like list of lines :

51
Hi, i'd like to have an easy way of doing IO operation in files :
my main 2 tasks are :
1. reading line from file. for this i've created the following function :
Expand|Select|Wrap|Line Numbers
  1.    sub read_line { 
  2.       my $filename = shift; 
  3.       my $index    = shift; 
  4.       open FILE, $filename or die "can't open $filename";
  5.       my @text = <FILE>;
  6.       return $text[$index];
  7.    }
  8.  
2. writing a text in specific line : in this part i'm having some difficulties to find the optimal way of implementation. i can do that by reading the whole file into list (as in the previous function), adding the requested line to that list, and overriding the original file with the revised list (see below).
Expand|Select|Wrap|Line Numbers
  1. sub write_line { 
  2.    my $filename = shift; 
  3.    my $index    = shift; 
  4.    my $text     = shift;
  5.    open FILE, $filename or die "can't open $filename";
  6.    my @file = <FILE>;   
  7.  
  8.    @file = (@file[0..($index-1)],"$text\n",@file[$index..$#file]);
  9.    print @file;
  10. }
  11.  
my question is whether there's a better way of doing these simple commands ?

thanks
Sep 27 '09 #1
4 1922
numberwhun
3,509 Expert Mod 2GB
@zcabeli
Before I get to your question I am going to say that having 44 posts under your belt, you are more than expected to know how to use code tags. I have corrected your post, but I think you need to please re-read the posting guidelines and use them correctly next time. If not, you will start accruing infractions.

As for your question, I would suggest that you write the new file information out to a temp file, that way you can verify it is doing its job correctly. You need to make sure you open that temp file for writing as well.

As for replacing the line, why not use a regex to find the line you want to replace and write out to the temp file the new line, when that line in question is found. And, if not, the lines pass through the test and get written out to file.

Regards,

Jeff
Sep 27 '09 #2
toolic
70 Expert
Consider using the http://perldoc.perl.org/Tie/File.html core module.
It allows you to perform array operations on lines of a file without having
to load the entire file into memory.
Sep 27 '09 #3
zcabeli
51
thanks for the help !
the tie package really suits my requirements.
btw, sorry for not following the guidelines this time, i'll make sure it won't happen again.
Sep 29 '09 #4
numberwhun
3,509 Expert Mod 2GB
@zcabeli
Much appreciated! :)
Sep 29 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: matt | last post by:
I am using a text file as a database, each field delimited by || I want to be able to print all lines to a page, then by selecting one (with, say, a radio button) it will load into the form at the...
0
by: Simon Burton | last post by:
# Needed to edit a text file like a list: file = TextFile( "foo.txt" ) del file # remove last 10 lines file.save() # Ended up writing this: #!/usr/bin/env python
5
by: Shu-Hsien Sheu | last post by:
Hi, Does the seek method would close the file object after using a for loop? My program looks like this: f = open('somefile', 'r') for lines in f: some operations f.seek(0)
22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
1
by: Angelos | last post by:
Hello, -I wrote a script that you use it to add products in the database. -Each product is associated with files. -In the Form to add the product you have to enter the product name in a textfield...
25
by: dixie | last post by:
I have some code that adds new records into a table for each ID in a list box when a button on a form is clicked. This works fine. My problem now is that I wish to be able to edit all the records...
10
by: setar | last post by:
How can I edit an xml file which has 250MB? I tried to use UltraEdit, Visual Studio, Eclipse, Stylus Studio and XMLSpy editors but these programs can't read this file because it is too big. SmEdit...
3
by: zbenta | last post by:
Hi you guys. I need some help with a program I've written. I can not seam to find the reason for the error that ocurrs. But let me explain. I have a txt file that works as a database to store...
0
by: bh | last post by:
I'm trying to edit data in a datagrid through a dropdown list with different id/text values. The problem comes in when I click the edit button & nothing appears to be happening. Did I do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.