lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Index lookups on strings will use the default set string metatable, which looks sort of like this: {__index = string}
Obviously string[3] and string[2] are empty by default.

If you want that effect you'll have to write your own string metatable that maps integer keys to sub and the rest to the regular __index lookup.

On Wed, Dec 16, 2009 at 2:29 PM, Hans Hagen <pragma@wxs.nl> wrote:
Hi,

The following is valid lua

a = "text"
b = a[3]
print(a[2])

but b as well as the print are nil. Is there a reason why
this is syntactically correct cq. does not map onto sub somehow?

I could not google info for this effect

Hans


-----------------------------------------------------------------
                                         Hans Hagen | PRAGMA ADE
             Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
    tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
                                            | www.pragma-pod.nl
-----------------------------------------------------------------