zoukankan      html  css  js  c++  java
  • Lua 可变参数之arg与select

     1 function TestFunc(...)
     2     local arg = { ... }                       --Lua 5.2以后不再支持默认arg参数,{}与...之间要有空格
     3     print("输入的参数个数:".. #arg)
     4     for i, v in ipairs(arg) do
     5         print(v)
     6     end
     7 end
     8 print("***************************************");
     9 function TestFunc2(...)
    10    print("输入的参数个数:".. select('#', ...))
    11     local count = select('#', ...);
    12     for i = 1, count do
    13         num = select(i, ...)
    14         print(num)
    15     end
    16 end
    17 TestFunc(1, 3, 5, 7, nil, 9)            --输出结果 1 3 5 7 9
    18 TestFunc2(1, 3, 5, 7, nil, 9)           --输出结果 1 3 5 7 nil 9

    通过代码可以得出结论:

    1.ipairs函数遇到nil停止输出

    2.把ipairs换成pairs 发现,输出结果是1 3 5 7 9,得出结论pairs会过滤到nil

    3.select(index,...)函数的作用是返回从第i个开始的所有元素,并且会输出nil

  • 相关阅读:
    Restful风格
    SpringMVC概念、原理及搭建
    Mybatis搭建
    HttpServletRequest、HttpServletResponse、Cookie、Session
    Servlet基础
    Spring整合Mybatis
    PHP代码标识
    IOC及Bean容器
    框架
    Spring概况
  • 原文地址:https://www.cnblogs.com/blackteeth/p/10155335.html
Copyright © 2011-2022 走看看