简体   繁体   English

尝试将 nil 与数字堆栈回溯进行比较?

[英]attempt to compare nil with number stack traceback?

I'm playing with Lua following this link: https://www.lua.org/pil/4.2.html and get confused about a point. I'm playing with Lua following this link: https://www.lua.org/pil/4.2.html and get confused about a point.

Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> x=10
> local i=1
> while i<=x do
>>  local x = i*2
>>  print(x)
>>  i=i+1
>> end
stdin:1: attempt to compare nil with number
stack traceback:
    stdin:1: in main chunk
    [C]: in ?

I guess this error message indicates that something is wrong with the expression while i<=x .我猜这个错误消息表明表达式while i<=x有问题。 Any comments are greatly appreciated.任何意见都非常感谢。 EDIT: I just realize that it's probably because it does not work in a terminal.编辑:我只是意识到这可能是因为它在终端中不起作用。

It did not work out in an interactive terminal.它在交互式终端中不起作用。 Because local i=1 is understood by terminal as a chunk by itself once you hit enter.因为一旦你按下回车, local i=1就会被终端理解为一个块。 That is why the "attempt to compare nil with number" error;这就是“尝试将 nil 与数字进行比较”错误的原因; because i is not defined, ie, nil in this case.因为 i 没有定义,即在这种情况下为零。 To correct it, put the first two lines and the while loop inside of a do chuck as the following.要更正它,请将前两行和 while 循环放在 do chuck 中,如下所示。

> do
>>  x = 10
>>  local i=1
>>  while i<=x do
>>    local x = i*2
>>    print(x)
>>    i = i+1
>>  end
>> end
2
4
6
8
10
12
14
16
18
20
> 

Actually the problem is in local i=1 try实际上问题出在local i=1尝试

> local i = 1
> print(i)

The problem is that when running the console, it seems that the line is a chunk and the variable is local inside that chunk.问题是,在运行控制台时,该行似乎是一个块,而变量是该块内的本地变量。 you can fix that by using a global variable or you can do this你可以通过使用全局变量来解决这个问题,或者你可以这样做

> local i = 1 do
>> print(i)
>> end

Which results in chunk structure like this [local i [print(i)]] therefore i can be accessed.这导致像这样的块结构[local i [print(i)]]因此可以访问 i。 Note also that local x = i*2 is valid since it is inside the while - do chunk.另请注意, local x = i*2是有效的,因为它在while - do块内。

Your code would also work correctly, if it was inside a Lua file.如果它位于 Lua 文件中,您的代码也可以正常工作。

I could reproduce the problem in Lua 5.3.4 as well.我也可以在 Lua 5.3.4 中重现该问题。

If you read on in the Lua docs, chapter 4.2 – Local Variables and Blocks , you'll get to the sentence如果您继续阅读Lua 文档,第 4.2 章 – 局部变量和块,您将看到这句话

Beware that this example will not work as expected if you enter it in interactive mode.请注意,如果您在交互模式下输入此示例,它将无法按预期工作。 The second line, local i = 1, is a complete chunk by itself.第二行,local i = 1,本身就是一个完整的块。

This addresses exactly the issue in question.这正好解决了所讨论的问题。 So it seems that the Lua interpreter has limited support for an outmost chunk (that is clearly present in a Lua file).因此,Lua 解释器似乎对最外层块的支持有限(显然存在于 Lua 文件中)。 But this behaviour seems to me acceptable and understandable in view of the compactness of language and interpreter.但鉴于语言和解释器的紧凑性,这种行为在我看来是可以接受和可以理解的。

So, when in interactive mode,所以,在交互模式下,

either leave out the local before variable i to make it work:要么在变量i之前省略local变量以使其工作:

Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
Lua>x=10
Lua>i=1
Lua>while i<=x do
...>local x=i*2
...>print(x)
...>i=i+1
...>end

or start enclose the whole by a block:开始用一个块包围整个:

Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
Lua>do
...>local x=10
...>local i=1
...>while i<=x do
...>local x=i*2
...>print(x)
...>i=i+1
...>end
...>end

Both options will produce to the regular (and expected) output:这两个选项都将产生常规(和预期)output:

2
4
6
8
10
12
14
16
18
20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM