buzzard51
buzzard51

Reputation: 1467

lua tables - string representation

as a followup question to lua tables - allowed values and syntax:

I need a table that equates large numbers to strings. The catch seems to be that strings with punctuation are not allowed:

local Names = {
   [7022003001] = fulsom jct, OH
   [7022003002] = kennedy center, NY
}

but neither are quotes:

local Names = {
   [7022003001] = "fulsom jct, OH"
   [7022003002] = "kennedy center, NY"
}

I have even tried without any spaces:

local Names = {
   [7022003001] = fulsomjctOH
   [7022003002] = kennedycenterNY
}

When this module is loaded, wireshark complains "}" is expected to close "{" at line . How can I implement a table with a string that contains spaces and punctuation?

Upvotes: 0

Views: 363

Answers (1)

Aki
Aki

Reputation: 2928

As per Lua Reference Manual - 3.1 - Lexical Conventions:

A short literal string can be delimited by matching single or double quotes, and can contain the (...) C-like escape sequences (...).

That means the short literal string in Lua is:

local foo = "I'm a string literal"

This matches your second example. The reason why it fails is because it lacks a separator between table members:

local Names = {
   [7022003001] = "fulsom jct, OH",
   [7022003002] = "kennedy center, NY"
}

You can also add a trailing separator after the last member.

The more detailed description of the table constructor can be found in 3.4.9 - Table Constructors. It could be summed up by the example provided there:

a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }

I really, really recommend using the Lua Reference Manual, it is an amazing helper.

I also highly encourage you to read some basic tutorials e.g. Learn Lua in 15 minutes. They should give you an overview of the language you are trying to use.

Upvotes: 2

Related Questions