参数隐式"arg"被指定为0.在Lua中无法正常工作 [英] Parameter implicit "arg" in functions not work in Lua

查看:204
本文介绍了参数隐式"arg"被指定为0.在Lua中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在函数中使用隐式参数arg时遇到问题.

该代码不起作用. http://www.lua.org/pil/5.2.html 文档,应该可以.

function listar_um (...)
  for i,v in ipairs(arg) do
    print("usando args " .. arg[i])  
  end
end
listar_um("Olá", 1, "Dois")

此代码与声明变量lista一起使用.

function listar_um (...)
  lista = {...}

  for i,v in ipairs(lista) do
    print("não usando args " .. lista[i])  
  end
end
listar_um("Olá", 1, "Dois")

为什么第一个示例不起作用?

测试脚本: http://www.codeshare.io/IPwRJ 执行在线脚本: http://www.compileonline.com/execute_lua_online.php

谢谢.

解决方案

PiL的第一版讨论Lua 5.0. arg的使用在Lua 5.0中可用,而自Lua 5.1起已将其删除

您可以在 Lua 5.0参考手册中找到它,但不在 Lua 5.1参考手册中.

在线解释器使用的版本是Lua 5.2,您可以通过print(_VERSION)来查找


编辑:经过一些测试,似乎arg在Lua 5.1中仍然可用,但在Lua 5.2中不起作用.

I have a problem in use implicit parameter arg in functions.

The code not works. The documentation, http://www.lua.org/pil/5.2.html, should works.

function listar_um (...)
  for i,v in ipairs(arg) do
    print("usando args " .. arg[i])  
  end
end
listar_um("Olá", 1, "Dois")

This code works with the declaring variable lista.

function listar_um (...)
  lista = {...}

  for i,v in ipairs(lista) do
    print("não usando args " .. lista[i])  
  end
end
listar_um("Olá", 1, "Dois")

Why the first example does not work?

Script for test: http://www.codeshare.io/IPwRJ Execute on-line script: http://www.compileonline.com/execute_lua_online.php

Thanks.

解决方案

The first edition of PiL talks about Lua 5.0. The use of arg is available in Lua 5.0, while it's removed since Lua 5.1

You can find it in Lua 5.0 reference manual, but not in Lua 5.1 reference manual.

The edition the online interpreter uses is Lua 5.2, you can find out by print(_VERSION).


Edit: after some tests, it seems that arg is still available in Lua 5.1, but not working in Lua 5.2.

这篇关于参数隐式"arg"被指定为0.在Lua中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆