Language Translator | Lua

Dynamic Range and Unit Formatter

This Lua script dynamically generates a result string based on pressure, temperature, and size ranges from a given model, appending appropriate units if provided. It ensures meaningful output by checking for null values and formatting


Empty image or helper icon

Prompt

local result = ''

if model['PressureRange'] and not ScriptHelpers.IsNullOrEmpty(model['PressureRange']) then
    result = model['PressureRange']['Name'] 
end 

if model['TemperatureRange'] and not ScriptHelpers.IsNullOrEmpty(model['TemperatureRange']) then
    result =  model['TemperatureRange']['Name'] 
end

if result == '' then 
    if model['Smax'] != model['Smin'] and (model['Smax'] !=0 and  model['Smin'] != 0) then
            result = model['Smin']..'...'..model['Smax']
    end
end 

if result !=''  then 
    if not ScriptHelpers.IsNullOrEmpty(model['VUnits'])  and model['VUnits']['Name'] != '-'  then    
        result = result .. ' ' .. model['VUnits']['Name']
    end 
end

if  ScriptHelpers.IsNullOrEmpty(model['VUnits']) and not ScriptHelpers.IsNullOrEmpty(model['SUnits']) and  model['SUnits']['Name'] != '-' then
    result = result .. ' ' .. model['SUnits']['Name']
end

return result

Answer

local result = ''

if model['PressureRange'] and not ScriptHelpers:IsNullOrEmpty(model['PressureRange']) then
    result = model['PressureRange']['Name']
end

if model['TemperatureRange'] and not ScriptHelpers:IsNullOrEmpty(model['TemperatureRange']) then
    result = model['TemperatureRange']['Name']
end

if result == '' then
    if model['Smax'] ~= model['Smin'] and (model['Smax'] ~= 0 and model['Smin'] ~= 0) then
        result = model['Smin'] .. '...' .. model['Smax']
    end
end

if result ~= '' then
    if not ScriptHelpers:IsNullOrEmpty(model['VUnits']) and model['VUnits']['Name'] ~= '-' then
        result = result .. ' ' .. model['VUnits']['Name']
    end
end

if ScriptHelpers:IsNullOrEmpty(model['VUnits']) and not ScriptHelpers:IsNullOrEmpty(model['SUnits']) and model['SUnits']['Name'] ~= '-' then
    result = result .. ' ' .. model['SUnits']['Name']
end

return result

Explanation:

  1. Initialization:

    • local result = ''
  2. Pressure Range Check:

    • Checks if model['PressureRange'] is not null/empty and assigns result.
  3. Temperature Range Check:

    • Checks if model['TemperatureRange'] is not null/empty and assigns result.
  4. Range Condition:

    • Checks if result is still empty, and if model['Smax'] and model['Smin'] are not equal and both non-zero, assigns a range to result.
  5. Result Suffix for VUnits:

    • Appends VUnits name to result if not null/empty and not '-'.
  6. Result Suffix for SUnits:

    • Appends SUnits name to result if VUnits is null/empty and SUnits is not null/empty and not '-'.

Note:

  • Lua uses the ~= operator for inequality, unlike != in some other languages.
  • Lua uses the : syntax for calling methods of objects (e.g., ScriptHelpers:IsNullOrEmpty).
  • The code is written in a compact and readable format consistent with Lua best practices.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This Lua script dynamically generates a result string based on pressure, temperature, and size ranges from a given model, appending appropriate units if provided. It ensures meaningful output by checking for null values and formatting conditions.