PtokaX forum

Archive => Archived 4.0 boards => Help with Lua 4 scripts => Topic started by: JAM on 23 August, 2004, 19:04:59

Title: Delete certain line in file... (dcdm)
Post by: JAM on 23 August, 2004, 19:04:59
I'm trying to delete row no. from a certain file, using lua5 in dcdm. I'm however still very unfamiliar with the lua language, so I'm not really sure what to base my searches on trying to find the solution, so I'm hoping for guidance here...

Sofar I'm guessing one of the following (hope it enough to understand):

* Read each line into tmp (array?) during a loop and if i == linenumber, skip it and continue. In the end, truncate the file, and loop tmp to re-write the info...

* Read the file, at the same write the strings i get into a tmp-file, except for the mentioned line. In the end renaming the file.

* Make a single write("") when i == linenumber.

I'd post example code but I don't have it locally. I have tried all above myself, but I just cannot get it to work correctly. =/ Thanks in advance.
Title:
Post by: BottledHate on 23 August, 2004, 20:37:27
i think this is what u are tryign to do??? not sure
though... this reads a file.. removes the line number
 specified.. then re-saves the file without that line.


-------------------
local filename = "scripts/bleh.txt"
local lineToRemove = 10
-------------------
local lineN = 1
local tmp =""
for line in io.lines(filename) do
   if lineN ~= lineToRemove then
      tmp = tmp..line.."\r\n"
   end
lineN = lineN +1
end
local f,e = io.open( filename, "w+" )
if f then
   f:write( tmp )
   f:close()
end
-------------------

-BH
Title:
Post by: JAM on 23 August, 2004, 21:47:11
Precicely, thank you. It suddenly looks so easy, but I just couldn't figure it out... =/
Title:
Post by: BottledHate on 23 August, 2004, 21:56:52
np.. i'm here to help when i can... a month ago i wouldn't
have been able to help you...  ;) some good sources for learning are:

Plops Webpage (http://www.plop.nl) -down atm.. but should be back up in a day or so.
Lua 5 Reference (http://www.lua.org/manual/5.0/manual.html)
and here on the forums in the Cleint Side Threads (http://board.univ-angers.fr/board.php?boardid=24&sid=5cad0f592e09c7aca3db2610661a7fe3)

-BH