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 ➜ add char to variable on condition

add char to variable on condition

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


Posted by Dagaswolf   USA  (9 posts)  Bio
Date Thu 26 Feb 2009 03:10 AM (UTC)
Message
hey there all...


this is what i am trying to do...
i have the following code...

tsth = tonumber(tsth)
tstm = tonumber(tstm)
tsts = tonumber(tsts)
teth = tonumber(teth)
tetm = tonumber(tetm)
tets = tonumber(tets)
	
local tch = (teth - tsth)
local tcm = (tetm - tstm)
local tcs = (tets - tsts)


what i am trying to do is calculate time... how long something takes basically...

**note** h=hour, m=minute, s=second **end note**

i want to have the output to look something like this:
00:00:00

and i know to output what i want i gotta do:

world.Send("echo "..tch..":"..tcm..":"..tcs)


the problem that i am finding is that when i get ONLY a 0-9 value in any of the variables, it outputs "0:0:0"

how do i do a check like:

if (string-length(tch) == 1) then
    tch = ([add-to-the-start-of-var]0 + tch)
end


any and all help is appreciated.

Dagaswolf -- The Super-Hytech-Redneck...
Top

Posted by WillFa   USA  (525 posts)  Bio
Date Reply #1 on Thu 26 Feb 2009 03:15 AM (UTC)
Message
string.format is what you're looking for.


print(  string.format( "%02d", 5) )  -->  05

%d is a number
%2d is two places for that number
%02d is pad with zeros if the number doesn't fill 2 places.

More information is in the help or
http://www.gammon.com.au/scripts/doc.php?lua=string.format


Top

Posted by Dagaswolf   USA  (9 posts)  Bio
Date Reply #2 on Thu 26 Feb 2009 03:29 AM (UTC)
Message
AWESOME!!!

that worked wonderfully!


might i ask another question?

i am now running into getting "-" values in my time.
how do i compensate for calculating time? like when it says 17 for the seconds in the start time, and 14 for the seconds in the end time... i get a value set of -3 seconds...

you know of any way to compensate for that?

Dagaswolf -- The Super-Hytech-Redneck...
Top

Posted by WillFa   USA  (525 posts)  Bio
Date Reply #3 on Thu 26 Feb 2009 03:52 AM (UTC)
Message
have you thought about something like:


tsth = tonumber(tsth)
tstm = tonumber(tstm)
tsts = tonumber(tsts)
teth = tonumber(teth)
tetm = tonumber(tetm)
tets = tonumber(tets)
starttime= tsth * 3600  + tstm * 60 + tsts
endtime  = teth * 3600  + tetm * 60 + tets

elapsedtime = endtime - start time
elh= math.floor(elapsedtime/3600)
elm= math.floor(elapsedtime - elh*3600)
els= elapsedtime - (elh*3600) - (elm*60)

print ( string.format("Time Taken: %0d:%02d:%02d", elh, elm, els) )



Top

Posted by Dagaswolf   USA  (9 posts)  Bio
Date Reply #4 on Thu 26 Feb 2009 04:06 AM (UTC)
Message
i think i see where your going with that...

but here is the output from that with the following times:

start time: 23:45:43
end time: 00:02:15

Time Taken: -24:992:-58528

the output don't make any sense to me ???

the output should say something like:
00:17:32
i think that my calculation is right for the time... yes?

Dagaswolf -- The Super-Hytech-Redneck...
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #5 on Thu 26 Feb 2009 04:31 AM (UTC)
Message
No, if it wraps around midnight. You might check that if the number is negative, add 24 hours to it.

- Nick Gammon

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

Posted by Dagaswolf   USA  (9 posts)  Bio
Date Reply #6 on Thu 26 Feb 2009 04:43 AM (UTC)
Message
ok... understand about the 24hour thing... but this is what i am getting now...

start time: 00:38:32
end time: 00:39:05

using the scripting that was given earlier:

local starttime = (tsth * 3600  + tstm * 60 + tsts)
local endtime = (teth * 3600  + tetm * 60 + tets)

local elapsedtime = (endtime - starttime)
local elh = math.floor(elapsedtime/3600)
local elm = math.floor(elapsedtime - elh*3600)
local els = (elapsedtime - (elh*3600) - (elm*60))

print ( string.format("Time Taken: %0d:%02d:%02d", elh, elm, els) )


this is the output that i am getting...
Time Taken: 0:33:-1947


shouldn't the "Time Taken:" show 00:00:35???


Dagaswolf -- The Super-Hytech-Redneck...
Top

Posted by Dagaswolf   USA  (9 posts)  Bio
Date Reply #7 on Thu 26 Feb 2009 04:55 AM (UTC)
Message
ok breaking it down... this is what i get for calculations.

local starttime = (tsth * 3600  + tstm * 60 + tsts)
local endtime = (teth * 3600  + tetm * 60 + tets)

local elapsedtime = (endtime - starttime)
local elh = math.floor(elapsedtime/3600)
local elm = math.floor(elapsedtime - elh*3600)
local els = (elapsedtime - (elh*3600) - (elm*60))

print ( string.format("Time Taken: %0d:%02d:%02d", elh, elm, els) )


starttime = ((0*3600) + (38*60) + 32) which = 2312
endtime = ((0*3600) + (39*60) + 5) which = 2345

elapsedtime = (2345 - 2312) which = 33

elh = 33/0 which = 0
elm = (33 - (0*60) = 0
els = (33 - (0*3600) - (0*60) = 33

so when i do the print i should get:
"Time Taken: 00:00:33"

is that not correct? but the output is: "Time Taken: 0:33:-1947"

Am i missing something???

Dagaswolf -- The Super-Hytech-Redneck...
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #8 on Thu 26 Feb 2009 04:55 AM (UTC)
Message
Try this:


tsth = 23
tstm = 45
tsts = 43

teth = 0
tetm = 02
tets = 15


local starttime = tsth * 3600  + tstm * 60 + tsts
local endtime = teth * 3600  + tetm * 60 + tets

local elapsedtime = endtime - starttime

if elapsedtime < 0 then
  elapsedtime = elapsedtime + 24 * 60 * 60
end -- if midnight wrap

local elh = math.floor (elapsedtime/3600)  -- hours
elapsedtime = elapsedtime - elh * 3600 -- remove hours
local elm = math.floor (elapsedtime/60)  -- minutes
elapsedtime = elapsedtime - elm * 60 -- remove minutes
local els = elapsedtime   -- seconds

print ( string.format("Time Taken: %0d:%02d:%02d", elh, elm, els) )


Testing on the test fields gave me:


Time Taken: 0:16:32


... which looks about right.

- Nick Gammon

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

Posted by WillFa   USA  (525 posts)  Bio
Date Reply #9 on Thu 26 Feb 2009 04:58 AM (UTC)
Message
I was just about to reply that I forgot to divide by 60 in elm...

Top

Posted by Dagaswolf   USA  (9 posts)  Bio
Date Reply #10 on Thu 26 Feb 2009 05:00 AM (UTC)
Message
AWESOME!!!! that worked...

thank you so much for your help!!! i really do appreciate it.

i am still really new to this scripting stuff. and that is why i am trying to do as much of it as i can instead of using others scripts. i want to learn

thanks again to both of you!

Dagaswolf -- The Super-Hytech-Redneck...
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.


32,049 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.