Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Fri May 24, 2019 5:14 am Post subject: Check Logic And Syntax Structure (Lua) [SOLVED] |
|
|
I am on a project to make a unit converter using CE Lua.
GUI: 3 Combo boxes and 2 edit boxes.
Say, I have 2 flat Lua tables.
Code: | local Angle = {'Degree','Gradian','Radian'}
local Area = {'Acres','Hectares','Square centimeter','Square feet','Square inch',
'Square kilometer','Square meter','Square mile','Square milimeter',
'Square Yard'} |
I need someone to help check the logic for the script below:
Code: | ---- Category
cbbox1.Items.Add('Angle')
cbbox1.Items.Add('Area')
function cbUnit() ---- append items to cbbox2 and cbbox3 by cbbox1 index (category)
local cb2 = cbbox2
local cb3 = cbbox3
local cat = cbbox1.ItemIndex
local itemsCB2 = cbbox2.Items
local itemsCB3 = cbbox3.Items
if cat == nil then return nil end
if cat == 0 then ------------------------ Angle
cb2.Items.clear()
cb3.Items.clear()
for i,v in ipairs(Angle) do
strings_add(itemsCB2, v)
strings_add(itemsCB3, v)
end
cb2.ItemIndex = 0
cb3.ItemIndex = 0
end
if cat == 1 then ------------------------ Area
cb2.Items.clear()
cb3.Items.clear()
for i,v in ipairs(Area) do
strings_add(itemsCB2, v)
strings_add(itemsCB3, v)
end
cb2.ItemIndex = 0
cb3.ItemIndex = 0
end
end
cbbox1.onChange = cbUnit
edit1.Text = 'Enter value' ---- default
function eClick() ---- make edit1.Text empty and wait user iinput
edit1.Text = ''
edit2.Text = ''
end
edit1.onClick = eClick
function edtChange() --- check user input
local val1, val2
val1 = edit1.Text
if val1 == '' then return nil end
if tonumber(val1) == nil then
edit1.Text = ''
return nil
else
val1 = val1
end
return val1
end
function unitCalc()
edtChange()
local input = edit1.Text
local output = edit2.Text
local cat = cbbox1.ItemIndex
local cb2Index = cbbox2.ItemIndex
local cb3Index = cbbox3.ItemIndex
--===========================================================================--
if cat == 0 then -- Angle (3 indexs)
if cb2Index == 0 and cb3Index == 0 then -- Angle = Degree to Degree
output = input
elseif cb2Index == 0 and cb3Index == 1 then -- Angle = Degree to Gradian
output = tonumber(input) * 1.11111111111111
elseif cb2Index == 0 and cb3Index == 2 then -- Angle = Degree to Radian
output = tonumber(input) * 0.0174532925199433
------------------------------------------------------------------------------
elseif cb2Index == 1 and cb3Index == 0 then -- Angle = Gradian to Degree
output = tonumber(input) * 0.9000000000000000
elseif cb2Index == 1 and cb3Index == 1 then -- Angle = Gradian to Gradian
output = input
elseif cb2Index == 1 and cb3Index == 2 then -- Angle = Gradian to Radian
output = tonumber(input) * 0.0157079632679489
------------------------------------------------------------------------------
elseif cb2Index == 2 and cb3Index == 0 then -- Angle = Radian to Degree
output = tonumber(input) * 57.2957795130823
elseif cb2Index == 2 and cb3Index == 1 then -- Angle = Radian to Gradian
output = tonumber(input) * 63.6619772367581
elseif cb2Index == 2 and cb3Index == 2 then -- Angle = Radian to Radian
output = input
end
--===========================================================================--
edit1.Text = input
edit2.Text = output
end
end
edit1.onChange = unitCalc
cbbox2.onChange = unitCalc
|
Until this part, all script works fine. Calculation result has show on edit2.Text.
But if I add/change the next script below, 'function unitCalc' seems not to work.
Code: | function unitCalc()
edtChange()
local input = edit1.Text
local output = edit2.Text
local cat = cbbox1.ItemIndex
local cb2Index = cbbox2.ItemIndex
local cb3Index = cbbox3.ItemIndex
--===========================================================================--
if cat == 0 then -- Angle (3 indexs)
if cb2Index == 0 and cb3Index == 0 then -- Angle = Degree to Degree
output = input
elseif cb2Index == 0 and cb3Index == 1 then -- Angle = Degree to Gradian
output = tonumber(input) * 1.11111111111111
elseif cb2Index == 0 and cb3Index == 2 then -- Angle = Degree to Radian
output = tonumber(input) * 0.0174532925199433
------------------------------------------------------------------------------
elseif cb2Index == 1 and cb3Index == 0 then -- Angle = Gradian to Degree
output = tonumber(input) * 0.9000000000000000
elseif cb2Index == 1 and cb3Index == 1 then -- Angle = Gradian to Gradian
output = input
elseif cb2Index == 1 and cb3Index == 2 then -- Angle = Gradian to Radian
output = tonumber(input) * 0.0157079632679489
------------------------------------------------------------------------------
elseif cb2Index == 2 and cb3Index == 0 then -- Angle = Radian to Degree
output = tonumber(input) * 57.2957795130823
elseif cb2Index == 2 and cb3Index == 1 then -- Angle = Radian to Gradian
output = tonumber(input) * 63.6619772367581
elseif cb2Index == 2 and cb3Index == 2 then -- Angle = Radian to Radian
output = input
end
elseif cat == 1 then -- Area (9 indexs)
if cb2Index == 0 and cb3Index == 0 then -- Area = Acres to Acres
output = input
elseif cb2Index == 0 and cb3Index == 1 then -- Area = Acres to Hectares
output = tonumber(input) * 0.40468564224
elseif cb2Index == 0 and cb3Index == 2 then -- Area = Acres to Squarecentimeters
output = tonumber(input) * 40468564.224
elseif cb2Index == 0 and cb3Index == 3 then -- Area = Acres to Squarefeet
output = tonumber(input) * 43560
elseif cb2Index == 0 and cb3Index == 4 then -- Area = Acres to Squareinch
output = tonumber(input) * 6272640
elseif cb2Index == 0 and cb3Index == 5 then -- Area = Acres to Squarekilometers
output = tonumber(input) * 0.0040468564224
elseif cb2Index == 0 and cb3Index == 6 then -- Area = Acres to Squaremeter
output = tonumber(input) * 4046.8564224
elseif cb2Index == 0 and cb3Index == 7 then -- Area = Acres to Square mile
output = tonumber(input) * 0.0015625
elseif cb2Index == 0 and cb3Index == 8 then -- Area = Acres to Square millimeter
output = tonumber(input) * 4046856422.400
elseif cb2Index == 0 and cb3Index == 9 then -- Area = Acres to Square Yard
output = tonumber(input) * 4840
------------------------------------------------------------------------------
elseif cb2Index == 1 and cb3Index == 0 then -- Area = Hectares to Acres
output = tonumber(input) * 2.47105381467165
elseif cb2Index == 1 and cb3Index == 1 then -- Area = Hectares to Hectares
output = input
elseif cb2Index == 1 and cb3Index == 2 then -- Area = Hectares to Square centimeters
output = tonumber(input) * 100000000
elseif cb2Index == 1 and cb3Index == 3 then -- Area = Hectares to Square feet
output = tonumber(input) * 107639.104167097
elseif cb2Index == 1 and cb3Index == 4 then -- Area = Hectares to Square inch
output = tonumber(input) * 15500031.000062
elseif cb2Index == 1 and cb3Index == 5 then -- Area = Hectares to Square kilometers
output = tonumber(input) * 0.01
elseif cb2Index == 1 and cb3Index == 6 then -- Area = Hectares to Square meter
output = tonumber(input) * 10000
elseif cb2Index == 1 and cb3Index == 7 then -- Area = Hectares to Square mile
output = tonumber(input) * 0.00386102158542446
elseif cb2Index == 1 and cb3Index == 8 then -- Area = Hectares to Square millimeter
output = tonumber(input) * 10000000000
elseif cb2Index == 1 and cb3Index == 9 then -- Area = Hectares to Square Yard
output = tonumber(input) * 11959.9004630108
------------------------------------------------------------------------------
--- and so on until cb2Index == 9 and cb3Index == 9
end
edit1.Text = input
edit2.Text = output
end
end |
No output on edit2.Text. Where is my mistake?.
EDIT [SOLVED]:
Don't worry. The problem is not on 'unitCalc function'. Problem solved here:
Code: | --Delete this function:
function edtChange() --- check user input
local val1, val2
val1 = edit1.Text
if val1 == '' then return nil end
if tonumber(val1) == nil then
edit1.Text = ''
return nil
else
val1 = val1
end
return val1
end
-- and change
function unitCalc(input, output)
input = edrt1.Text
if input == '' or tonumber(input) == nil then
return nil
else
cat = cbbox1.ItemIndex
cb2Index = cbbox2.ItemIndex
cb3Index = cbbox3.ItemIndex
-- and so on until
edit1.Text = input
edit2.Text = output
end
end
|
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|