HTOTM: FUSION
Originally Posted by psycore View Post
This looks like you're running Lua in a console and not in Toribash. I am not too familliar with that.
What the error means is that the variable output doesn't exist. One reason could be that out.txt doesn't exist and cannot be created (Lua usually creates files if you try to open non existing files with write access). The other reason could be that local variables don't work in the console. Try doing it without "local" infront of "output".


GREAT!!!
Yes you are correct my LUA console was not taking the local variable, when I tried the same thing without "local" it worked.... Thanks alotttt!!!


I have one more question:
if I open one file in the read and write mode I want to search one word which is there in that file several times... How can I do that... I want to add one more line next to that word.....


Please help, Thanks in advance!!
I just noticed I didn't quite make clear that io.open("file", "w") doesn't open the file in read/write mode but in write only, so you can't read from it.

Now to your question. The easiest way is this:
input = io.open("file", "r")
text = input:read("*a")
input:close()
text = string.gsub(text, "wordToReplace", "replacement")
output = io.open("file", "w")
output:write(text)
output:flush()
output:close()
gsub() replaces text so if you wanted to add "\n bar" behind "foo" you have to do
text = string.gsub(text, "foo", "foo\n bar")
Last edited by psycore; Jul 6, 2012 at 01:35 PM. Reason: Not quite awake while writing that
Signature temporarily out of order.
(1). NO Sir, I think I was not able to convey my question correctly......
my question is something to add some more lines after the end of the specific line... and not to replace anything...

(2) Here I will explain my task for which I am preparing all these pieces of code.
I have one xml file in my local -- I will open it -- say it is giving me info about one equipment from one of its node -- say te equipment is ruler -- it has some 4 parameters -- unit of measure is one of the parameter and in my file it is meter -- now I have to add simillar sets in the same file -- but with centimeter and milimeter -- so one node will expand to three nodes -- yes, the info is same in all the three-- @ same time I need not to disturb rest of the two parameters
This is my task ... can you give some clue... I am not expecting a full code, I know you will be busy but it would be great if you provide me some guideline.

(3) Yes.. this is also a request... can you please tell me where I will get the bunch of the methods about file input output, strings ( like you used one for replacement here) or about other topics in LUA.


I know I am troubling you very much, but please help.....
First of all, you're not troubling me, that's what I'm here for.

(2) I'm not sure if I understood correctly. Could you give me an example code of how the .xml file looks before your operation and how it is supposed to look after it?

(3) There's a Lua IO Reference Manual
Signature temporarily out of order.
Thanks....

file1
---------------------
<?xml version="1.0" ?>
- <catalog>
- <equipment id="eq101">
<name>ruler</name>
<material>plastic Guide</material>
<measures>length</measures>
<unit>meter</meter>
<size>12</size>
</catalog>
I have this file. Now it should become like the below one
<?xml version="1.0" ?>
- <catalog>
- <equipment id="eq101">
<name>ruler</name>
<material>plastic Guide</material>
<measures>length</measures>
<unit>meter</meter>
<size>12</size>
- <equipment id="eq101">
<name>ruler</name>
<material>plastic Guide</material>
<measures>length</measures>
<unit>centimeter</meter>
<size>1200</size>
- <equipment id="eq101">
<name>ruler</name>
<material>plastic Guide</material>
<measures>length</measures>
<unit>milimeter</meter>
<size>12000</size>
</catalog>


If you will observe, equipment id is same for all the three node in new file..only the unit and size are changing accordingly...
Thanks in advance!!
The problem is you can't insert text into a file, you can only overwrite it. I suggest reading the file and storing all the information into a table like this:
equipments = {}
currentEquip = ""
for line in input:lines() do
    --[[
        if line contains equipment then
            -- get the id
            equipments[id] = {}
            currentEquip = id
        elseif line does not contain catalog then
            equipments[currentEquip][(string between < and >)] = (string between tags)
        end
    ]]--
end
After that, rewrite the whole file with the information you have.
Signature temporarily out of order.
ok.... I was planning some other approach, I will take complete one node in one string -- try to get the measures and unit -- then based on that I will produce other tags and then I will concatenanate the nodes.. please tell me how do you find this approach.... If possible give me some outline of this approach in terms of syntax...
For the regular expression part, see here, here or here (including next page). The last one is the best but also most complicated.

first = true
-- complete saves the whole text we want to write
complete = ""
-- tmp contains one node
tmp = ""
for line in input:lines() do
    --[[ pseudo code
        if line containes "equipment"
            -- if we find it the first time, there's no node to be saved yet
            if first then first = false
            else
                -- attach the node
                complete .. tmp .. "\n"
                -- change the node to centimeter
                in tmp replace meter with centimeter
                in tmp replace "<size>(%d+)" with "<size>%100" -- regular expression
                -- attach that part
                complete .. tmp .. "\n"
                -- change the node to milimeter
                in tmp replace centi with mili
                in tmp replace "<size>(%d+)" with "<size>%10" -- regular expression
                -- attach again
                complete .. tmp .. "\n"
            end
        -- if line is part of node attach to tmp
        elseif line doesn't contain catalog and line doesn't contain xml then
            tmp .. line .. "\n"
        else
            -- no part of node, directly attach to complete
            complete .. line .. "\n"
        end
    ]]--
end
-- output and close
input:close()
output:write(complete)
output:flush()
output:close()
This assumes that you have a input and a output variable which handle the files.
Signature temporarily out of order.