What is the alternative for the switch statement in Lua?

I have this piece of C ++ code, and I want to know how to write some codes that replace the switch statement in Lua, because I have a lot of problems, and I need to use this operator.

   int choice;
do// loop
{
      cout<<"\n >>> The General Menu <<< \n";
      cout << endl;
    cout<< " press (1) to Add    "<<endl;
    cout<< " press (2) to Save   "<<endl;
    cout<< " press (3) to Quit " << endl;
    cout<< endl;
      cout<< "Enter your choice please (1/2/3): ";
    cin>>choice;

    switch(choice)
    {
        case 1: 
            add();
            break;
        case 2:
            save();
            break;

        default:
            cout<<" The program has been terminated "<<endl;
           cout<<" Thank you! \n";          
    }   
} while (choice != 3);

} The operator is used inside the do..while loop.

+4
source share
6 answers

In general, if you want the switch statement in Lua, what you should do is create a table. For your simple case choice, which may be 1, 2 or a failure, a simple operator ifwith several conditions is enough . For more complex cases, use the function table:

local c_tbl =
{
  [1] = add,
  [2] = save,
}

local func = c_tbl[choice]
if(func) then
  func()
else
  print " The program has been terminated."
  print " Thank you!";
end

, , , inline.

+10

Lua:

if choice == 1
then add()
elseif choice == 2
then save()
else print "The program has been terminated\nThank you!"
end 
+1

( ):

local case=2;
local result=({[1]="case1", [2]="case2", 3="case3"})[case];
print (result); --> case2
+1

( , script Lua). , ; )

..!!

print("enter your choice : ")
mychoice = io.read()

switch = function (choice)
  -- accepts both number as well as string
  choice = choice and tonumber(choice) or choice     -- returns a number if the choic is a number or string. 

  -- Define your cases
  case =
   {
     [1] = function ( )                              -- case 1 : 
             print("your choice is Number 1 ")       -- code block
     end,                                            -- break statement

     add = function ( )                              -- case 'add' : 
             print("your choice is string add ")     -- code block
     end,                                            -- break statement

    ['+'] = function ( )                             -- case '+' : 
             print("your choice is char + ")         -- code block
     end,                                            -- break statement

     default = function ( )                          -- default case
             print(" your choice is din't match any of those specified cases")   
     end,                                            -- u cant exclude end hear :-P
   }

  -- execution section
  if case[choice] then
     case[choice]()
  else
     case["default"]()
  end

end
-- Now you can use it as a regular function. Tadaaa..!!
switch(mychoice)
+1

, , , - :

( switch , , . return switch, (case) )

(, , , Python, )

#!/usr/bin/lua
-- Callback switch statement:
local function switch(a, case)
  -- Local variable instead of function(a) on every case:
  local value = a
  -- Cases list:
  local switchcase = {}

  -- Cases:
  switchcase["string"] = function()
    return (tostring(value) .. " is a string")
  end

  switchcase["number"] = function()
    return tostring(value .. " is a number")
  end

  switchcase["boolean"] = function()
    return (tostring(avalue) .. " is a boolean")
  end

  return switchcase[type(case)](a)
end

local value = 5
print(switch(value,value)) --> 5 is a number

local value = "test"
print(switch(value,value)) --> test is a string

local value = true
print(switch(value,value)) --> true is a boolean

, . , .

0

loadstring() .

switch = function(cases,args)
  if (cases[args] == nil) then return args else return assert(loadstring ('return ' .. cases[args]))() end
end

local case = 2

local result = switch({
  [1] = "2^" .. case,
  [2] = string.format("2^%i",case),
  [3] = tostring(2^case)
},
case
)
print(result) --> 4

, loadstring() Python eval().

, "function (x)" , Lua. .

" " - "return args".

0

All Articles