Opening a file for write problem


I'd be grateful if anybody could help me with this problem as it's driving me mad!

Basically I have a flat text file called days.txt, in it are 7 lines, each corresponding to a day of the week seperated with the | symbol and a number:



I want to open the file, modify one of the numbers (dependant on some processing) then write the file away again.

My problem:

If I OPEN the file for WRITE, assign the contents to an array, then modify one line element then PRINT and CLOSE the file, I end up with a blank file other than one element (all existing contents are removed).

If I OPEN the file for APPEND, do the same, I get the amended line at the end of a blank file (regardless of what element I tell it to update).

And finally (obviously) if I OPEN the file READ ONLY, I can't write to it any way.

How do I open the file, read contents into the array, modify one array element, then write the file back keeping all the original data except the changed element?

Any help would be appreciated, although I probably won't check here until after Easter (Tuesday 02/04/02).


Who is Participating?
$^I is the inplace-edit extension.  Setting it is like running perl with the -i switch.
from `perldoc perlrun` the -i switch
                      specifies  that  files  processed by the <> construct
                      are to be edited in-place.  It does this by  renaming
                      the  input  file,  opening  the  output  file  by the
                      original name, and selecting that output file as  the
                      default  for  print()  statements.  The extension, if
                      supplied, is used to modify the name of the old  file
                      to make a backup copy, following these rules:
                      If  no  extension  is supplied, no backup is made and
                      the current file is overwritten.
You didn't say which element you wanted to modify, or how you wanted to modify it,
so as an example
s/(Tue\|)(\d+)/$1.($2+1)/e; #changes the line "Tue|1" to "Tue|2"
Open the file for write, read the WHOLE FILE into memory, make your change (you might need to write a function to search for the day that corresponds to the number you want to modify, and return the position of the number), then write the whole array to the file.


Thank you for posting your comment.

As a new member of the EE community, Welcome!!

While posting a comment, it best not to propose it as an answer, unless you are 100% certain that this is the solution the user is looking for.

Even then most people prefer not to lock the question by selecting the "Answer" button, for the 2 main reasons.

1) Locking questions denys other users from seeing the question in the main area and therefore deprives the user of more alternatives.
2) Since we all learn more by sharing, we deny ourselves a chance to learn and increase our knowledge horizons.

Therefore, i would humbly request you to take back your answer and open up the question to all users.

Thanks in advance,

(NOT a moderator at EE)
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

{local $^I=".bak"; local @ARGV=("filename");
  while( <> ){
For reasons stated above, proposed answer rejected.

E-E Moderator

NTIVERAuthor Commented:
ozo - your solution looks very complicated, although judging from your massive 202991 points in the Perl topic area, I can probably safely assume that you know what you're talking about :-)

As I am quite new to the Perl language, could you just explain a little bit more about what your suggestion is doing?  I'm OK with variables and arrays but am finding it difficult to follow your solution through.

Could you just guide me through the parts please?

Many thanks in advance for your help.


Please post the final solution that you use here.

This will help us all know the answer that you were looking for.

Thanks in advance,
you have to open a file for read close it and then open it again to write it..
i suggest you open the file to read keep the number you wanna change in a scalar and close the file
than you open the file agai for write and than you use substitute.. example (s/$1/$2)
for the one you wanna change with the number you kept can post the script i'll fix it to you
NTIVERAuthor Commented:
ozo - I get it now, thanks for the explanation.

Let me have a play with your suggestion(s) and I'll get back and sort the points out (assuming it works <bg>) as soon as I can.


NTIVERAuthor Commented:
ozo - you're suggestion works for the purposes of what I need - thank you very much, sorry it took a while to get back to you.

NTIVERAuthor Commented:
ozo - you're suggestion works for the purposes of what I need - thank you very much, sorry it took a while to get back to you.

NTIVERAuthor Commented:
LioBSD - thank you for your comments also - I've used your suggestion in a new script I'm developing which has other requirements.

Thank you everyone else ;)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.