[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: String indexing again
- From: Michal Kottman <k0mpjut0r@...>
- Date: Mon, 20 Dec 2010 14:48:26 +0100
On Mon, 2010-12-20 at 14:03 +0100, Axel Kittenberger wrote:
> > s[3] is being used to mean the 3rd character of s.
>
> Not?
It is used in the manual to denote the 3rd byte in the string. It does
not work in Lua like that. Strings have their metatable __index pointed
to the 'string' table, imagine like the following was done when
initializing Lua:
setmetatable("", {__index=string})
Therefore, indexing a string doesn't result in an error, like it did in
previous versions. You can type s[3] in Lua, but it will always return
nil, because there is no such entry in the 'string' table. For example:
n = 1
print(n[1])
-- stdin:1: attempt to index global 'n' (a number value)
s = "1"
print(s[1])
-- nil, because strings have a metatable __index pointing to the string
table, and string[1] is nil