Yad Smood
Yad Smood

Reputation: 2942

Lua key name starts with digit in table statement

When key name starts with digit, in javascript we can define array-like object like this:

var table = {
    '123.com': 'details'
    '456.net': 'info'
}

But when I try these code in Lua5.1:

table = { '123.com' = 'info' }

It throws an error:

[string "local"]:1: '}' expected near '='

But these code are accepted in lua:

table = {}
table['123.com'] = 'info'

I wonder if it is a bug in Lua5.1. Or did I missed something?

Upvotes: 1

Views: 306

Answers (1)

Jason LeBrun
Jason LeBrun

Reputation: 13303

When creating a Lua table using the literal table constructor, non-identifier table indices should be enclosed in square brackets. For example:

table = { ['123.com'] = 'info' }

(From: http://www.lua.org/pil/3.6.html)

Upvotes: 7

Related Questions