Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Trying to write to file

Trying to write to file

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Fleagor   (2 posts)  Bio
Date Thu 19 Mar 2009 02:47 AM (UTC)

Amended on Thu 19 Mar 2009 02:48 AM (UTC) by Fleagor

Message
Hello,

I have a trigger that fires off when the first line of an item identification comes up, and launches a script that I hoped would write the information to a file, up until a blank line. Here's what I'm using:

item_list = io.open("itemlist.txt", "a")

repeat
	line = io.read()
	item_list:write(line)
until line == ""

item_list:close()

Here's the error I'm getting:

Run-time error
Immediate execution
[string "Trigger: "]:5: bad argument #1 to 'write' (string expected, got nil)
stack traceback:
        [C]: in function 'write'
        [string "Trigger: "]:5: in main chunk

The file is created, but remains empty. I'm guessing it's a problem with reading in output from the world. What's the best way to pipe that data into a file?
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #1 on Thu 19 Mar 2009 04:02 AM (UTC)
Message
You can't use io.read to read from the output buffer. io.read reads from stdin, which is not really assigned to anything in a GUI application.

What I would do is, when the first line of your multi-line sequence arrives, clear a table, eg.


lines = {}


Then enable a trigger to capture the subsequent lines, until a blank line.

That trigger adds each line, eg.


table.insert (lines, "%0") -- insert another line


Then when you get the blank line which terminates it all, disable this trigger, and write the whole lot out, similar to what you have but instead of io.read, do:


for _, line in ipairs (lines) do
  item_list:write(line)
end --for


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Fleagor   (2 posts)  Bio
Date Reply #2 on Fri 20 Mar 2009 01:47 AM (UTC)
Message
Thanks! I think my original idea was to redirect io.stdout to the file, but then I couldn't think of how to point it back to the screen after, and it looks like I got way off track with that read function. Anyway, I found your inventory example in the FAQ, and combined with the changes you showed to the file writing loop, I'm pretty sure I'll have this up and running in no time.

I just wanted to say I'm really impressed with Mushclient, your presence in the forums, and the quality of your tutorials and responses. It's great to see.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


11,884 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.