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!

Add another line of data to TOP not to bottom? 1

Status
Not open for further replies.
Apr 13, 2006
9
GB
Hey thank you all for your help on my other problems youve been great.

Is it possible to write data to a flatfile but add it to the top of the list instead of under the others?.

This is what i use to write to the file:

print GFILE "$gametitle\n";

but it adds the entry to the bottom of the list, i want to add it at the top. Like move the other entries down and then add the new one at the top, can this be done and if so how please?.
 
No, not in the way you want (for normal files anyway). A rather clunky solution is to read the file into memory, close it, re-open it for writing, write the new line first, then write the rest of it back from memory, then close it. Of course, if there is a power cut half way through, your file is hosed...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
it is possible using the Tie::File module.


you might be able to do it using the inplace editor:

Code:
{ 
  local $^I = "";
  local @ARGV = $path_to_file;
  while (<>) { 
     print "insert before line 1\n" if $. == 1;
     print; 
  } 
}

or something similar to that, but you will have to test it out to see if it works.
 
you can use at the promt something like this
Code:
perl -pe '$i++||print "my new first line\n"' oldfile > newfile
but this will give you a totaly new file then of course you can move the new own to the old with
Code:
mv newfile oldfile
and you 're done.


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top