为什么 Lua 数组(表)从 1 而不是 0 开始? [英] Why do Lua arrays(tables) start at 1 instead of 0?

查看:50
本文介绍了为什么 Lua 数组(表)从 1 而不是 0 开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白Lua这部分决定背后的基本原理.为什么索引从 1 开始?我已阅读(和其他许多人一样)这篇很棒的论文.在我看来,这是一种学习和编程非常愉快的语言的一个奇怪的角落.不要误会我的意思,Lua 很棒,但必须在某处进行解释.我发现的大部分内容(在网络上)只是说索引从 1 开始.句号.

I don't understand the rationale behind the decision of this part of Lua. Why does indexing start at 1? I have read (as many others did) this great paper. It seems to me a strange corner of a language that is very pleasant to learn and program. Don't get me wrong, Lua is just great but there has to be an explanation somewhere. Most of what I found (on the web) is just saying the index starts at 1. Full stop.

阅读设计者对这个主题的评价会很有趣.

It would be very interesting to read what its designers said about the subject.

请注意,我是 Lua 的非常"初学者,希望我没有遗漏一些关于表格的明显内容.

Note that I am "very" beginner in Lua, I hope I am not missing something obvious about tables.

推荐答案

Lua 源自 Sol,这是一种专为石油工程师设计的语言,没有经过计算机编程的正规培训.没有受过计算训练的人认为从零开始计数是非常奇怪的.通过采用基于 1 的数组和字符串索引,Lua 设计者避免混淆他们的第一批客户和赞助商的期望.

Lua is descended from Sol, a language designed for petroleum engineers with no formal training in computer programming. People not trained in computing think it is damned weird to start counting at zero. By adopting 1-based array and string indexing, the Lua designers avoided confounding the expectations of their first clients and sponsors.

虽然一开始我也觉得它们很奇怪,但我已经学会了喜欢基于 0 的数组.但是我对 Lua 的基于 1 的数组感到满意,尤其是通过使用 Lua 的通用 for 循环和 ipairs 运算符—我通常可以避免担心数组的索引方式.

Although I too found them weird at the beginning, I have learned to love 0-based arrays. But I get by OK with Lua's 1-based arrays, especially by using Lua's generic for loop and the ipairs operator—I can usually avoid worrying about just how arrays are indexed.

这篇关于为什么 Lua 数组(表)从 1 而不是 0 开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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