lua中利用table表模拟switch
来源:互联网 发布:检测ip端口是否打开 编辑:程序博客网 时间:2024/05/02 19:07
因为lua中没有switch,有滴童鞋可能觉得太多if elseif 看着难受,不过其实我们可以利用table去模拟switch的功能.
首先定义一个表
switch={}
然后定义各个分支
switch[1]=function()...end
switch[a]=function()...end
... ...
然后调用
switch[var]()
记得后面的()不要忘记哦!\(^o^)/~
switch={}
然后定义各个分支
switch[1]=function()...end
switch[a]=function()...end
... ...
然后调用
switch[var]()
记得后面的()不要忘记哦!\(^o^)/~
我们这里举个简单例子,把月份转换成汉字形式
switch={}switch[1]=function() return '一月' endswitch[2]=function() return '二月' endswitch[3]=function() return '三月' endswitch[4]=function() return '四月' endswitch[5]=function() return '五月' endswitch[6]=function() return '六月' endswitch[7]=function() return '七月' endswitch[8]=function() return '八月' endswitch[9]=function() return '九月' endswitch[10]=function() return '十月' endswitch[11]=function() return '十一月' endswitch[12]=function() return '十二月' endprint(switch[1]())
———得到的输出结果:
一月
参考资料:
http://bbs.uc.cn/thread-1988354-1-1.html
http://zuoge85.iteye.com/blog/827460 0 0
- lua中利用table表模拟switch
- python中模拟switch
- LUA中table表长度问题
- Lua中table介绍
- lua中table活用
- lua中Table数据结构
- Lua 中Table遍历
- lua中table总结
- lua中 table
- [寒江孤叶丶的Cocos2d-x之旅_40]在LUA中模拟switch
- 利用lua的元表追踪table变量的变化
- Lua 中模拟 类
- 【Lua】浅析Lua中table的遍历
- 【Lua】浅析Lua中table的遍历
- lua 中table 内嵌table
- Lua -- table(表)
- Lua table(表)
- Lua table表函数库
- 郁闷的C小加(二)
- oracle job 定时执行 存储过程
- MyEclipse Servers视窗出现“Could not create the view: An unexpected exception was thrown”错误解决办法
- 计算机图形学
- 纸质文件从PDF转化为word 教程
- lua中利用table表模拟switch
- C语言链表逆置
- 传输频率与符号率。。
- vs2012 error c4996: 'fopen': This function or variable may be unsafe
- Python内建函数(H)
- html中optgroup标签用法,分组、只读的且呈树状样式显示
- eclipse工程导入 android studio
- ubuntu登陆失败
- iOS中block实现的探究