[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: String indexing again
- From: Axel Kittenberger <axkibe@...>
- Date: Mon, 20 Dec 2010 14:56:14 +0100
Thank you, yes, IMHO writing the call explicitly instead of the ':'
notatoin, demonstrates the mechanics nicely
$ s = "AAA"
$ return s["byte"](s, 1)
52
On Mon, Dec 20, 2010 at 2:48 PM, Michal Kottman <k0mpjut0r@gmail.com> wrote:
> 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
>
>
>
>