Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Cofiguring [i] into a Digital Format? [closed]

Asked by 6 years ago

I have a basic for loop timer, only I'd like to present it to my String in the format of a digital clock I.e '0:00', how would I create the algorithm to Configure it to represent that way?

0
Include your attempt. xPolarium 1388 — 6y
0
what he said DinozCreates 1070 — 6y
0
It's nothing but Label.Text = this until this = that then do this this way. I.e, it's not even an algorithm, I'm so lost, I need a complete explanation User#25448 0 — 6y
0
unless you still want it User#25448 0 — 6y
View all comments (4 more)
0
You use string patterns and simple math to convert seconds to what ever time. Know that a for loop is usually for counting to a point so it's more like a timer than a clock. xPolarium 1388 — 6y
0
This question is trivial enough that as soon as you know how to do it (use mod), you've basically done it. Requesting an attempt for something somebody may have no idea how to even start is slightly unreasonable. fredfishy 833 — 6y
0
I know, I am using it to give the value to count down, duh. I just din't know how to format it into a constant algorithm, instead of having to use String patterns. Could you show me? User#25448 0 — 6y
0
And he responded that all he had was basic code. I followed with a concept he could try. xPolarium 1388 — 6y

Closed as Not Constructive by User#24403, DinozCreates, fredfishy, Azmidium, Leamir, and yHasteeD

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
4
Answered by
fredfishy 833 Moderation Voter
6 years ago
Edited 6 years ago
for time = 1, 100 do
    -- % (modulo) gets the remainder of a division
    -- This gives the number of seconds
    seconds = time % 60

    -- If we take away the number of seconds, what we get is exactly divisible
    --   by 60
    remainingTime = time - seconds
    minutes = remainingTime / 60

    -- Format as a string
    x = tostring(minutes) .. tostring(seconds)
end

This gives us almost what we want, but sometimes the time "1:5" is returned, rather than "1:05", for example.

--[[
  Function to pad a string with 0s on the left to reach a minimum length
  text: string -> The text to pad on the left with 0s
  targetLength: integer -> The minimum length of the string we'll return
  return -> Input text, left padded with 0s to a minimum of targetLength.
--]]
function zfill(text, targetLength)
    -- Work out the number of digits required
    digitsRequired = targetLength - string.len(text)
    padding = string.rep("0", digitsRequired)
    return padding .. text
end

That should work to achieve the padding we want.

Combining everything we get:

--[[
  Function to pad a string with 0s on the left to reach a minimum length
  text: string -> The text to pad on the left with 0s
  targetLength: integer -> The minimum length of the string we'll return
  return -> Input text, left padded with 0s to a minimum of targetLength.
--]]
function zfill(text, targetLength)
    -- Work out the number of digits required
    digitsRequired = targetLength - string.len(text)
    padding = string.rep("0", digitsRequired)
    return padding .. text
end

for time = 1, 100 do
    -- % (modulo) gets the remainder of a division
    -- This gives the number of seconds
    seconds = time % 60

    -- If we take away the number of seconds, what we get is exactly divisible
    --   by 60
    remainingTime = time - seconds
    minutes = remainingTime / 60

    -- Format as a string
    minuteText = tostring(minutes)
    secondText = zfill(tostring(seconds), 2)
    print(minuteText .. ":" .. secondText)
end
0
Could you explain the zfill function, and why call. it zfill? User#25448 0 — 6y
0
zfill is just "zero fill". I stole the name from Python's in-built function which does the same thing. fredfishy 833 — 6y
0
It works by taking a bit of text and a target length. It tests the text's length. It needs to add (target length - actual length) zeros to get to the target length. string.rep just repeats a string a certain number of times, so string.rep("0", targetLength - actualLength) gives us the exact amount of padding we want. Then we just join everything together. fredfishy 833 — 6y
0
You should use the # length operator rather than string.len User#24403 69 — 6y
View all comments (7 more)
0
What's the difference? I presume that one just calls the other under the hood. fredfishy 833 — 6y
0
There's just no reason to use len anymore with the length operator, plus it works with tables User#24403 69 — 6y
0
But it's not a table, and will never be a table. You can't generalise out the function to left-pad any iterable, because string.rep is a necessary part. Your argument consists of "way A exists so don't use way B", but the exact same argument could work in reverse. There's just no reason to use "#" on strings when "string.len" exists. fredfishy 833 — 6y
0
In short, it smells like you're just finding faults for the sake of finding faults, rather than actually bringing anything constructive to the table. fredfishy 833 — 6y
0
name one occurrence in where len is preferable over # :\ User#24403 69 — 6y
0
Name one where # is preferable over len. They do the exact same thing. Unless you can provide me with some sort of manual page or benchmark showing that for some ridiculous reason one has worse asymptotic time complexity than the other, I fail to see any need to say one's better than the other. fredfishy 833 — 6y
1
You should use string.format like string.format("d", tostring(number)) instead of your zfill function. You can go to https://en.wikipedia.org/wiki/Printf_format_string for more information. hiimgoodpack 2009 — 6y
Ad