[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  f:write doesn't want to write
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

f:write doesn't want to write

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


Posted by Ogomez92   (42 posts)  [Biography] bio
Date Tue 01 Dec 2009 03:32 PM (UTC)
Message
Hi,
I've been trying to get thi to work for over 2 hours now and I just don't seem to get the point.
I'mt rying to get a script to write soemthing to a file, like this:
f=io.input(GetInfo(67).."/db.txt")
dbw=f:write("hey\n")
Note(dbw)
This note returns nil
and then the file is the same as it was
I want to append hey to the end of that file because there's already something written in it.
Help would be appreciated. Thanks.
[Go to top] top

Posted by Cage_fire_2000   USA  (119 posts)  [Biography] bio
Date Reply #1 on Tue 01 Dec 2009 05:44 PM (UTC)

Amended on Tue 01 Dec 2009 05:46 PM (UTC) by Cage_fire_2000

Message
I'm not an expert, but it could be that it's because you opened the file for /input/ which pretty much makes it read-only not output. You might want to use io.open() instead.

Edit: From the help file, I'm guessing you want either mode "a" or "a+" for appending.
[Go to top] top

Posted by Ogomez92   (42 posts)  [Biography] bio
Date Reply #2 on Tue 01 Dec 2009 06:16 PM (UTC)
Message
Oh, my bad! Thanks for that. I was opening it as input so I couldn't write to it. Doh!
[Go to top] top

Posted by Ogomez92   (42 posts)  [Biography] bio
Date Reply #3 on Tue 01 Dec 2009 07:45 PM (UTC)
Message
Hi, sorry peeps but I'm having another frustrating issue.
I got it to write down the value of a variable just fine, but no matter what I do, I can't seem to get it to write a newline. I tried concatinating a newline to the variable like this: f:write(GetVariable("var").."\n") and it tells me unfinished string. I also tried concatinating it directly after the variable outside the write function and still, unfinished string. I even tried doing the var first which works fine and then in another function f:write. Btw, all these bracesare ment to be backslashes, dunno why it doesn't let me typethem on this site. I'm pasting the trigger hereb ecause I don't know what's wrong. The thing is that it should read a file, see if the character who jsut logged on is recognized, otherwise configure its prompts etc.

dbplayers={}
db=io.input(GetInfo(67).."/db.txt")
line=db:read("*l") -- read one line
while line do -- if not end of file (EOF)
table.insert(dbplayers,line)
line = db:read ("*l") -- read one line
end
db:close()
me=GetVariable("me")
if (IsInTable(dbplayers,GetVariable("me"))~=1) then
dbw=io.open(GetInfo(67).."/db.txt","a+")
dbw:write(GetVariable("me").."\n")
dbw:close()
Note("config mud")
else
Note("recognized.")
end
[Go to top] top

Posted by Cage_fire_2000   USA  (119 posts)  [Biography] bio
Date Reply #4 on Tue 01 Dec 2009 08:04 PM (UTC)

Amended on Tue 01 Dec 2009 08:05 PM (UTC) by Cage_fire_2000

Message
Hmm, I'm not sure, I've never used the file io routines in Lua, I suppose you could try changing "\n" to "\r\n", that's the DOS code for a new line, I suppose "\n" might not be registering as a newline, although I'm not sure if that matters. I know from recent experience on my system I need to use "\r\n" when appending to a notepad window.

I'm not sure what you're trying to do. Are you keeping some sort of global list of player names for the different worlds?
[Go to top] top

Posted by Ogomez92   (42 posts)  [Biography] bio
Date Reply #5 on Tue 01 Dec 2009 08:08 PM (UTC)
Message
Hmm nope. Sadly, \r\n does not work either.
dbw:write(GetVariable("var"),"\r\n")
Compile error
Plugin: AlterAeon (called from world: input)
Immediate execution
[string "Trigger: "]:18: unfinished string near '"'
Btw jus wondering why does this forum convert all the backslashes to \? At least it does in the box where you write...
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #6 on Tue 01 Dec 2009 08:17 PM (UTC)

Amended on Tue 01 Dec 2009 08:18 PM (UTC) by Twisol

Message
If you have forum codes enabled, it converts any \ that's not a valid forum code into \\. (Manually typing those so that you see what I mean, I have to double the backslashes up.)

Triggers and other things with send boxes don't do well with backslashes. The reason is that MUSHclient parses the content once before the scripting engine gets to it. So, \\\\" becomes \\" when MUSHclient begins parsing it, which then becomes \" when Lua parses it. So... try using four slashes instead of one when you're editing a trigger.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #7 on Tue 01 Dec 2009 08:18 PM (UTC)

Amended on Tue 01 Dec 2009 08:19 PM (UTC) by David Haley

Message
Ogomez92 said:

Btw jus wondering why does this forum convert all the backslashes to \? At least it does in the box where you write...

Isn't a backslash supposed to be \ ? Maybe you have strange font settings or something, but I see your backslashes coming out as backslashes...

But, this does make me wonder if we're seeing what we're supposed to be seeing here. Could you try posting your code to pastebin.com to make sure we're all on the exact same page? If Lua thinks that the string is unfinished, it could very well be because of a missed escape or something like that.

Also, where exactly are you entering all this? (in MC that is)



EDIT: Ah, listen to Twisol. :-)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Ogomez92   (42 posts)  [Biography] bio
Date Reply #8 on Tue 01 Dec 2009 09:24 PM (UTC)

Amended on Tue 01 Dec 2009 11:43 PM (UTC) by Nick Gammon

Message
Does that mean that I should make it f:write(whatever.."\\n")?
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #9 on Tue 01 Dec 2009 09:33 PM (UTC)
Message
Yes. I think you should also disable forum codes in your posts, because - as I said - the four slashes you posted get squished down to two, so we see two when you wrote four.

I.e. what we see in that post is wrong (two slashes), but what I'm sure you intended is correct (four slashes).

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Wed 02 Dec 2009 02:26 AM (UTC)

Amended on Wed 02 Dec 2009 02:27 AM (UTC) by Nick Gammon

Message
In the "send" box, you need to use \\r\\n to get a carriage-return linefeed (because MUSHclient changes \\ to \ and then Lua changes \r to carriage-return and \n to linefeed).

In a script file you don't need to do that because there is no preprocessing (in other words just use \r\n).

As for appending to a file, see:

http://www.gammon.com.au/scripts/doc.php?lua=io.open

I would be opening with mode "a" (append to existing).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] 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.


9,329 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]