Fix the formatting to include leading zeros, when necessary - datetime

I'm making a countdown timer. The output isn't showing leading zeros on hours/minutes/seconds less than 10.
I think maybe I need to use string.format somehow, but I'm new to lua and I'm not sure how.
function GetTimeLeft()
local dif = os.time(RELEASEDATE) - os.time()
local timeleft = {
[1] = math.floor(dif/60/60/24), --day
[2] = math.floor(dif/60/60)%24, --hour
[3] = math.floor(dif/60)%60, --minute
[4] = math.floor(dif)%60 --second
}
local text = {}
for i=1, #timeleft do
if i == 1 then
if timeleft[i] > 0 then
table.insert(text,timeleft[i])
end
else
table.insert(text,timeleft[i])
end
end
if dif <= 0 then
text = RELEASETEXT
else
text = table.concat(text,":")
end
return tostring(text)
end
Expected 8:02:08:05
Actual 8:2:8:5

Related

When running code in R was given an error that there was a missing value where true/false needed and I can't fix it

I am new to using R and have minimal amount of Python experience. I am sure this is an easy fix but I am just not seeing it. I was given a code to run a Fibonacci sequence to 100 and I copy and pasted it, but I am getting the following error code: Error in if (numterms <= 0) { : missing value where TRUE/FALSE needed. I know this has to do with the if/else clause but I am not seeing the problem.
I have run through the code a couple different ways but it has not helped. And the person to assist is not available during the weekend. Any help would be appreciated.
# take the max number input from the user
numterms = as.integer(readline(prompt="What is your max number? "))
# first two items
num1 = 0
num2 = 1
counter = 2
# check if the number of terms is valid
if(numterms <= 0) {
print("Please enter an integer above zero")
} else {
if(numterms == 1) {
print("The Fibonacci sequence:")
print(num1)
} else {
print("The Fibonacci sequence:")
print(num1)
print(num2)
while(counter < numterms) {
numth = num1 + num2
print(numth)
# update values
num1 = num2
num2 = numth
counter = counter + 1
}
}
}
If you just execute the code numterms is not correctly defined. It is normally defined by a user input: The function readline reads the numbers the user types in the command line. If you just execute this line you can properly define numterms.
If you execute all the code at once numterms is set to NA which cannot be compared to 0 in the numterms <= 0 clause. In this case numterms <= 0 is also NA which is not a logical value and can therefore not be evaluated by if. This ultimately causes your error.
The solution would be to just run the first line of your code and enter the number and only after you entered the number to execute the rest of the code.
Alternatively you can define your code as a function:
printFibonacci <- function(){
numterms = as.integer(readline(prompt="What is your max number? "))
if(is.na(numterms)){
numterms <- 4
}
# first two items
num1 = 0
num2 = 1
counter = 2
# check if the number of terms is valid
if(numterms <= 0) {
print("Please enter an integer above zero")
} else {
if(numterms == 1) {
print("The Fibonacci sequence:")
print(num1)
} else {
print("The Fibonacci sequence:")
print(num1)
print(num2)
while(counter < numterms) {
numth = num1 + num2
print(numth)
# update values
num1 = num2
num2 = numth
counter = counter + 1
}
}
}
}
And then just call your function with printFibonacci(). In this case the prompt and answer of the readline function gets executed first and numterms can be defined by the user before the rest of the code is executed.

Getting a zero value for a variable that clearly should not be zero

I have written the following code to calculate temperature distribution along a fin. For some reason, it keeps calculating certain values as zero, even when they aren't!
for t = 0:300:3000
for i = 2:L2
if i ==2
A(i)= 0.75*rhocp*DX/DT + 3*k/dx;
B(i)= k/dx;
C(i)= 2*k/dx;
D(i)= 0.75*rhocp*(DX/DT)*T(i);
elseif i == L2
A(i)= 0.75*rhocp*DX/DT + 3*k/dx;
B(i)= 2*k/dx;
C(i)= k/dx;
D(i)= 0.75*rhocp*(DX/DT)*T(i);
else
A(i)= 0.75*rhocp*DX/DT + 2*k/dx;
B(i)= k/dx;
C(i)= k/dx;
D(i)= rhocp*(DX/DT)*T(i);
end
P(1) = 0;
Q(1) = T(1);
for i = 2:L2
DENO = A(i) - C(i)*P(i-1);
NUM = D(i)+ C(i)*Q(i-1);
P(i) = B(i)/DENO;
Q(i) = NUM/DENO;
end
for i =L2:-1:2
if i == L2
T(i) = Q(i);
else
T(i) = P(i)*T(i+1) + Q(i);
end
end
T(L1) = ((2*k*T(L2) - h*DX*Tinf)/(2*k - h*DX));
end
disp (T);
`
DENO and NUM is calculated as zero in the first iteration, even though on calculating their values is not zero! This leads to "Division by zero" error.
A(2)-C(2)*P(1)
ans =
3750.
Analytically it has got a value though.
Please give the parameter values so one can run your script...
DT was missing , i set it to 1 .
Finally I found the problem: you forgot an end to close the loop
for i = 2:L2
the end has to be put just before P(1)=0
Moreover this loop does not depend on the t value so it could be put before the loop for t = 0:300:3000

HTTP parser for Lua

I'm trying to parse http POST requests in Lua. My implementation works, but eats a hell lot of CPU load. This is critical, hence it is on an embedded platform.
I've looked on other implementations, but they can't fit, because my image is hardly fit in the memory, so I wouldn't use another library. I rolled my own parser, but it uses too much of system resource. Question is how could I optimize this to have lower CPU load.
This is an OpenWRT based system, so I only have Lua 5.1. This is the core function that looks for the boundary (in str variable). It reads the input block by block, and seeks for it.
The other solution would be to use LUCI libraries to do the heavy lifting, but I don't want my code to be integrated in LUCI.
--look for a pattern (str) and copy input until it is found to the output.
local function writeuntil(in_fp, str, out_fp)
local buff = ""
local ret = false
local bs = 4096 --Block size. The amount of data to read at once
local c = in_fp:read(bs)
local strStartPos = 1
while c do
local blockLen = string.len(c) --Not sure that a whole block is read, so get the size of the actual block.
local found = string.find(c, str, 1, true) --Try to locate str, so we don't have much work.
if (found ~= nil) then
if found > 2 then
out_fp:write(string.sub(c, 1, found - 1))
end
ret = true
break --we are done
else --Try to mach str till the end of the block
local strPos = string.find(c, string.sub(str, strStartPos, strStartPos), 1, true) --try to locate the first character
if strPos then --There is a starting character in the block
if (strPos > 1) then
out_fp:write(string.sub(c, 1, strPos - 1))
end
for i = strPos, blockLen do --iterate through the block
local ch = string.sub(c, i, i)
if ch == string.sub(str, strStartPos, strStartPos) then
buff = buff .. ch
if string.len(buff) == string.len(str) then
ret = true
break --We're done
end
strStartPos = strStartPos + 1
else --Lost track. Output.
if string.len(buff) > 0 then
out_fp:write(buff)
buff = ""
end
out_fp:write(ch)
strStartPos = 1
end
end
else
out_fp:write(c)
end
end
if ret then
break
end
c = in_fp:read(bs) --read next block
end
return ret
end
Egor, you were right, but I ended up this solution. It is now uses much less CPU. This not perfect hence scp is faster (although that is implemented in C).
--look for a pattern (str) and copy input until it is found to the output.
local function writeuntil(in_fp, str, out_fp)
local buff = ""
local ret = false
local bs = 4096 --Block size. The amount of data to read at once
local c = in_fp:read(bs)
local strStartPos = 1
local lastStrPos = 1
local needData = true
while c do
local blockLen = string.len(c) --Not sure that a whole block is read, so get the size of the actual block.
local found = string.find(c, str, 1, true) --Try to locate str, so we don't have much work.
if (found ~= nil) then
if found > 1 then
if #buff > 0 then
out_fp:write(buff)
end
out_fp:write(string.sub(c, 1, found - 1))
end
ret = true
break --we are done
else --Try to mach str till the end of the block
local strPos = string.find(c, string.sub(str, strStartPos, strStartPos), lastStrPos, true) --try to locate the first character
if strPos then --There is a starting character in the block
out_fp:write(string.sub(c, lastStrPos, strPos - 1))
for i = strPos, blockLen do --iterate through the block
local ch = string.sub(c, i, i)
if ch == string.sub(str, strStartPos, strStartPos) then
buff = buff .. ch
if string.len(buff) == string.len(str) then
ret = true
break --We're done
end
strStartPos = strStartPos + 1
lastStrPos = i + 1
else --Lost track. Output.
if string.len(buff) > 0 then
out_fp:write(buff)
buff = ""
end
out_fp:write(ch)
strStartPos = 1
if i == blockLen then
needData = true
else
lastStrPos = i + 1
needData = false
end
break
end
end
else
if ret == false then
if string.len(buff) > 0 then
out_fp:write(buff)
buff = ""
end
out_fp:write(string.sub(c, lastStrPos))
lastStrPos = 1
needData = true
else
break
end
end
end
if ret then
break
end
if needData then
c = in_fp:read(bs) --read next block
lastStrPos = 1
end
end
return ret
end

Classic ASP : what's wrong with my code to convert quantity from dozen to piece (eg. 10.3 dozen = 123 pieces)

What i want is to retrieve quantity in database from piece and covert it to dozen. Then input as dozen and convert back to pieces and save to database again.
when I input data eg. 10.3, it should convert to 123 piece for me ((10 * 12) + 3). My code work well without my "If clause" but only when data was "single" type. It made error when I input integer number, so I added "If.." statement to check it first which is now the output was correct for Integer but incorrect when I input single number.
I have this code..
Function DzToPcs(val)
'If CLng(val) = val then <-- not work
'if Fix(val) <> val then <-- work but the output was not correct when input single type number.
if Int(vInt) = vInt then <-- work but the output was not correct when input single type number.
DztoPcs = val * 12
else
strInt = Cstr(val)
a = Split(strInt,".")
dz = a(0)
pcs = a(1)
getdz = Cint(dz)
getpcs = Cint(pcs)
DztoPcs = (getdz * 12) + getpcs
end if
I'm not sure what's wrong with your if statements (my VBScript is a little rusty), but you could try this alternative:
Function DzToPcs(val)
strInt = Cstr(val)
a = Split(strInt,".")
dz = a(0)
if UBound(a) > 0 then
pcs = a(1)
getdz = Cint(dz)
getpcs = Cint(pcs)
DztoPcs = (getdz * 12) + getpcs
else
DztoPcs = dz * 12
end if
end function

Can Do While Loop stop when counter reaches X

I want to edit some legacy code written in classic-ASP.
Currently I've got a subroutine declared that uses a for-next loop to output some radio buttons:
For i = 1 to Cols
response.write "blah"
...
Next
i is simply a counter, Cols is a value passed to the sub-routine. I tried editing the for-loop to be a while loop instead:
i = Start
do while i <= Cols
response.write "blah"
...
i = i + 1
loop
But I get a Response Buffer Limit Exceeded error. If I replace Cols with the value it works fine. Is this a limitation in classic-ASP?
Reason I want to use a do while loop is because currently the sub-routine is limited to looping from 1 to Cols. It would be helpful to sometimes specify the loop counts backwards (i.e. step -1) but I can't write:
if Direction = Backwards then
For i = Cols to 1 step -1
else
For i = 1 to Cols
end if
How about:
If Direction = Backwards Then
cs = 10
ce = 1
s = -1
Else
cs = 1
ce = 10
s = 1
End If
For i = cs To ce Step s
''
Next

Resources