1. Data File:
We can use the table constructor in Lua to define a file format, that is, the data in the file is constructed and initialized by table.Code, This method for LuaProgramIt is very convenient and clear, such:
Entry {"Stephen Liu", "male", "programmer", "BS "}
Entry {"Jerry Tian", "male", "programmer", "BS "}
Note that entry {<code >}is equivalent to entry ({<code>}). If we can define an appropriate entry function for the preceding data entries, we can make the data part of our Lua code. See the following code and its comments:
1 Local Count = 0
2 -- The entry function is pre-defined here to find the matching function when executing the data code in dofile.
3 Function Entry () Count = count + 1 End
4 Dofile ( " D:/lua_data.conf " )
5 Print ( " Number of entries: " . Count)
6
7 -- Output result:
8 -- Number of entries: 2
Compared with the preceding data file format, we can also define a clearer "Custom Data" format, where each data item is accompanied by a brief description of its meaning. Using this format, even if data items change in the future, we can still maintain backward compatibility with minimal changes. See the following data format and related code:
Entry {name = "Stephen Liu", Gender = "male", job = "programmer", education = "BS "}
Entry {name = "Jerry Tian", Gender = "male", job = "programmer", education = "BS "}
1 Local Personinfo = {}
2 Function Entry (B)
3 -- Here, the name field value of table object B is used as the key information of personinfo.
4 If B. Name Then
5 Personinfo [B. Name] = True
6 End
7 End
8
9 Dofile ( " D:/lua_data.conf " )
10 For Name In Pairs (Personinfo) Do
11 Print (Name)
12 End
13
14 -- Output result:
15 -- Jerry Tian
16 -- Stephen Liu
We can see that these code snippets all adopt the event-driven approach. As a callback function, the entry function is called for each entry in the data file when dofile is executed.
Lua not only runs fast, but also compiles quickly. This is mainly because Lua uses data description as one of the main applications of Lua at the beginning of design.
2. serialization:
I believe that people with Java or C # development experience are familiar with this term. It is to convert the data object to a byte stream and then output it to a file or network through Io. when reading the data, it is re-constructed as a new object with the same value as the original object. Alternatively, we can use an executable Lua code as the serialized data format. For example, varname = <expr>. <expr> indicates the expression used to calculate the varname variable. The following sample code is used to serialize a non-circular table:
1 Function Serialize (o)
2 If Type (O) = " Number " Then
3 Io. Write (O)
4 Elseif Type (O) = " String " Then
5 -- The "% Q" parameter of the string. Format function can escape the metacharacters in the string.
6 Io. Write ( String. Format ( " % Q " , O ))
7 Elseif Type (O) =" Table " Then
8 Io. Write ( " {\ N " )
9 -- Iterates each element in the table and recursively writes the value of each field.
10 -- We can see that this simple example supports nested tables.
11 For K, V In Pairs (O) Do
12 -- This is done to prevent K from containing illegal Lua identifiers.
13 Io. Write ( " [ " ); Serialize (k ); Io. Write ( " ] = " )
14 Serialize (V)
15 Io. Write ( " , \ N " )
16 End
17 Io. Write ( " } \ N " )
18 Else
19 Error ( " Cannot serialize " .. Type (O ))
20 End
21 End