[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]  Leading zeros wanted.
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Leading zeros wanted.

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


Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Tue 15 May 2007 03:10 AM (UTC)
Message
I'm trying to have a script display time in a format like 2m:05s in a script. Unfortunately, I can't find a nice way to add a zero in front of the seconds if it is less than 10 seconds without a separate if statement. Is there a string format option better than string.format( "%2dm:%2d", min, sec ) for what I'm trying to do, or should I give up and put an if statement in and just make a string out of it?

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #1 on Tue 15 May 2007 03:30 AM (UTC)

Amended on Tue 15 May 2007 03:31 AM (UTC) by David Haley

Message
[david@thebalrog:~]$ cat test.lua
#!/usr/bin/lua


str = string.format("%02d %02d %03d", 0, 1, 2)
print(str)


[david@thebalrog:~]$ lua test.lua
00 01 002


That is what you want, no?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

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

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #2 on Tue 15 May 2007 03:34 AM (UTC)
Message
Yes, precisely what I want to do. Thanks... been a while since I've needed to use those C formats.

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #3 on Tue 15 May 2007 09:42 PM (UTC)

Amended on Tue 15 May 2007 09:45 PM (UTC) by Shaun Biggs

Message
I'll just continue with using this to ask for string format help... Can anyone tell me what's wrong with this?
  <alias
   match="chancap:list"
   send_to="12"
   regexp="y"
   enabled="y"
  >
  <send>for i,v in pairs( channels ) do
  ColourNote( "white", "black", string.format( "%10s", tostring(i) ),
              "lime",  "black", " :"..v )
  end
  </send>
  </alias>

currently this spits out:
0s :true
0s :true
0s :true

if I get rid of the string.format bit, and just have i, then it shows the correct keys and values, but it's all messy looking.

Oh, and the value of channels = { ["clan"] = "true", ["ftalk"] = "true", ["spouse"] = "true" }

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #4 on Wed 16 May 2007 12:20 AM (UTC)
Message
I don't get this behavior:


[dhaley@visionary:~]$ cat test.lua

channels = { ["clan"] = "true", ["ftalk"] = "true", ["spouse"] = "true" }

for i,v in pairs(channels) do
    print(string.format("%10s", tostring(i)), " :" .. v)
end

[dhaley@visionary:~]$ lua test.lua
    spouse       :true
     ftalk       :true
      clan       :true


The key/val pairs it prints out are exactly the ones in the table.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

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

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #5 on Wed 16 May 2007 12:21 AM (UTC)
Message
Oh. Is it possible that MUSHclient is doing a preprocessing step and translating %1 to the value of the first match, which is empty, and so the string.format string is actually just "0s" because the %1 gets removed?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

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

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #6 on Wed 16 May 2007 02:08 AM (UTC)
Message
Yep, that did it. I sent everything to a separate script, and it seems to be working fine now. I'm starting to think I should pay some sort of attention while scripting from now on. That explains why copying the syntax from one function to the script in the alias didn't act the same at all.

It is much easier to fight for one's ideals than to live up to them.
[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.


12,217 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]