Lua used as an example of a complete data description language

We look at two examples to illustrate the use of the full Lua language.
The first example comes from the Lua website, he demonstrated the use of Lua as a data description language.
The second example to explain the algorithm implemented Markov chain, this algorithm works Practice Kernighan & Pike of Programming book also described. After two complete example, introduction of language Lua will be ended. Table described later and will continue to object-oriented content and standard library, C-API like.

Lua used as a data description language

Lua Lua site keeps a project created using the example of a database containing all over the world. We in the database for each entry point to project automatic archiving manner as a constructor, as follows:

entry{ 
title = "Tecgraf", 
org = "Computer Graphics Technology Group, PUC-Rio", 
url = "http://www.tecgraf.puc-rio.br/", 
contact = "Waldemar Celes", 
description = [[ 
TeCGraf is the result of a partnership between PUC-Rio, the Pontifical Catholic University of Rio de Janeiro, and <A HREF="http://www.petrobras.com.br/">PETROBRAS</A>, the Brazilian Oil Company. TeCGraf is Lua's birthplace, and the language has been used there since 1993. Currently, more than thirty programmers in TeCGraf use Lua regularly; they have written more than two hundred thousand lines of code, distributed among dozens of final products.]] 
} 

Interestingly, the list of the project entry is stored in a Lua file, each project entry in the form of table as a parameter to call the entry functions. Our goal is to write a program that will show the data out in html format. Due to too many projects, we listed first title of the project, and then displays details of each project. The results are as follows:

<HTML> 
<HEAD><TITLE>Projects using Lua</TITLE></HEAD> 
<BODY BGCOLOR="#FFFFFF"> 
Here are brief descriptions of some projects around the world that use <A HREF="home.html">Lua</A>. 
<UL> 
<LI><A HREF="#1">TeCGraf</A> 
<LI> ... 
</UL> 
<H3> 
<A NAME="1" 
 HREF="http://www.tecgraf.puc-rio.br/">TeCGraf</A> 
<SMALL><EM>Computer Graphics Technology Group, 
PUC-Rio</EM></SMALL> 
</H3> 
TeCGraf is the result of a partnership between 
... 
distributed among dozens of final products.<P> 
Contact: Waldemar Celes 
<A NAME="2"></A><HR> 
... 
</BODY></HTML> 

To read the data, we need to do is correctly defined function entry, and then use the data file can be run directly dofile (db.lua). Note that we need to traverse the list of entries twice, the first time in order to obtain the title a second time in order to obtain representation of each project. One method is: the same operational data entry functions once all files placed in the inlet of an array; Another method: Run data files using two different entry function. Because Lua is compiled files quickly, where we use the second method.

First, we define the auxiliary function is used to output a formatted text (see Part function 5.2)

function fwrite (fmt, ...) 
return io.write(string.format(fmt, unpack(arg))) 
end 
第二,我们定义一个 BEGIN 函数用来写 html 页面的头部 
 
function BEGIN() 
io.write([[ 
<HTML> 
<HEAD><TITLE>Projects using Lua</TITLE></HEAD> 
<BODY BGCOLOR="#FFFFFF"> 
Here are brief descriptions of some projects around the 
world that use <A HREF="home.html">Lua</A>. 
]]) 
end 
第三,定义 entry 函数
a. 第一个entry函数,将每个工程一列表方式写出,entry的参数o是描述工程的table。
function entry0 (o) 
 N=N + 1 
local title = o.title or '(no title)'
 fwrite('<LI><A HREF="#%d">%s</A>\n', N, title) 
end 
如果 o.title 为 nil 表明 table 中的域 title 没有提供,我们用固定的"no title"替换。b. 第二个 entry 函数,写出工程所有的相关信息,稍微有些复杂,因为所有项都是可选的。
function entry1 (o) 
 N=N + 1 
local title = o.title or o.org or 'org'
 fwrite('<HR>\n<H3>\n') 
local href = ''
 if o.url then 
 href = string.format(' HREF="%s"', o.url) 
 end 
 fwrite('<A NAME="%d"%s>%s</A>\n', N, href, title) 
 if o.title and o.org then 
 fwrite('\n<SMALL><EM>%s</EM></SMALL>', o.org) 
 end 
 fwrite('\n</H3>\n') 
 if o.description then 
 fwrite('%s', string.gsub(o.description, 
 '\n\n\n*', '<P>\n')) 
 fwrite('<P>\n') 
 end 
 if o.email then 
 fwrite('Contact: <A HREF="mailto:%s">%s</A>\n', 
 o.email, o.contact or o.email) 
 elseif o.contact then 
 fwrite('Contact: %s\n', o.contact) 
 end 
end 
由于 html 中使用双引号,为了避免冲突我们这里使用单引号表示串。
第四,定义 END 函数,写 html 的尾部
function END() 
fwrite('</BODY></HTML>\n') 
end 
在主程序中,我们首先使用第一个 entry 运行数据文件输出工程名称的列表,然后再以第二个 entry 运行数据文件输出工程相关信息。
BEGIN() 
N = 0 
entry = entry0 
fwrite('<UL>\n') 
dofile('db.lua') 
fwrite('</UL>\n') 
N = 0 
entry = entry1 
dofile('db.lua') 
END()
Published 257 original articles · won praise 152 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_39885372/article/details/104349625
Recommended