Leading zeros wanted.

Posted by Shaun Biggs on Tue 15 May 2007 03:10 AM — 7 posts, 34,793 views.

USA #0
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?
USA #1
[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?
Amended on Tue 15 May 2007 03:31 AM by David Haley
USA #2
Yes, precisely what I want to do. Thanks... been a while since I've needed to use those C formats.
USA #3
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" }
Amended on Tue 15 May 2007 09:45 PM by Shaun Biggs
USA #4
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.
USA #5
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?
USA #6
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.