people playing: 206
servers online: 59
games played: 73,273,483

  Toribash Community » Toribash » Mods » Lua scripts
Reply
 
Thread Tools Display Modes
Old Jul 5, 2012   #461
pranavkulkarni
Junior Member
 
Join Date: Jul 2012



Originally Posted by psycore View Post
With Toribash you can only access files in the Toribash folder.
For the rest:
Code:
-- you can open many files at the same time:
local input = io.open("path/to/the/file.xml", "r") --read acces
local output = io.open("out.txt", "w") -- write acces

-- multiple ways to read:
-- line for line
for line in input:lines() do
    echo(line) -- print line to screen
end
-- cursor is now at end of file, let's put it back to the beginning
input:seek("set")
-- read whole file:
local text = input:read("*a")

input:seek("set")
-- read line:
local aLine = input:read("*l")
-- five chars:
local fiveChars = input:read(5)

--enough about reading
input:close()

-- now to writing. let's write the text of input to output:
output:write(text)

-- for demonstration let's put "LUA" at the middle of the file:
local fileSize = output:seek("end")
output:seek("set", math.floor(fileSize/2))
-- keep in mind that this overwrites the previous file content!
output:write("LUA")
output:flush
output:close

I did these fraction by fraction: I am getting results for most of them....
this was fine
> local output = io.open("out.txt", "w")
> local text = "hi this is the text file!!"

but how to chk the file is created or not?? what is the default location where the file get created?? can we change that??
-----
I tried to do the filesizing operation: it gave me this error:
> local fileSize = output:seek("end")
stdin:1: attempt to index global 'output' (a nil value)
stack traceback:
stdin:1: in main chunk
[C]: ?
then I decided to write directly:
> output:write("LUA")
this also gave me the error:
stdin:1: attempt to index global 'output' (a nil value)
stack traceback:
stdin:1: in main chunk
[C]: ?

please help Sir...
-----
I have one doubt, 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!!

Last edited by pranavkulkarni; Jul 5, 2012 at 03:49 PM.. Reason: <24 hour edit/bump
pranavkulkarni is offline   Reply With Quote
Old Jul 5, 2012   #462
psycore
Member
 
2nd Dan Black Belt
Join Date: Feb 2010
Posts: 267



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".
psycore is offline   Reply With Quote
Old Jul 5, 2012   #463
Yoyo
Raconteur
 
5th Dan Black Belt
Join Date: Nov 2009
Posts: 3,692
Clan: Tint



Is there any function equal to "sleep(time_in_seconds)" in toribash's lua?
!
<rooster12> I'm a brown belt, but still use nooby moves, like shoveling, any suggestions on how to stop?
<JSnuffMARS> where is the begging board?
<mineyo66> Does IRC use internet?
Yoyo is offline   Reply With Quote
Old Jul 5, 2012   #464
psycore
Member
 
2nd Dan Black Belt
Join Date: Feb 2010
Posts: 267



No
psycore is offline   Reply With Quote
Old Jul 6, 2012   #465
pranavkulkarni
Junior Member
 
Join Date: Jul 2012



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!!
pranavkulkarni is offline   Reply With Quote
Old Jul 6, 2012   #466
psycore
Member
 
2nd Dan Black Belt
Join Date: Feb 2010
Posts: 267



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:
Code:
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
Code:
text = string.gsub(text, "foo", "foo\n bar")

Last edited by psycore; Jul 6, 2012 at 12:35 PM.. Reason: Not quite awake while writing that
psycore is offline   Reply With Quote
Old Jul 6, 2012   #467
pranavkulkarni
Junior Member
 
Join Date: Jul 2012



(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.....
pranavkulkarni is offline   Reply With Quote
Old Jul 6, 2012   #468
psycore
Member
 
2nd Dan Black Belt
Join Date: Feb 2010
Posts: 267



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
psycore is offline   Reply With Quote
Old Jul 6, 2012   #469
pranavkulkarni
Junior Member
 
Join Date: Jul 2012



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!!
pranavkulkarni is offline   Reply With Quote
Old Jul 6, 2012   #470
psycore
Member
 
2nd Dan Black Belt
Join Date: Feb 2010
Posts: 267



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:
Code:
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.
psycore is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:50 AM.


Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
ragdoll fighting game physics fighting game ragdoll fighting physics funmotion joints martial arts karate pc mac free game turn based game