﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-cslover-GameFans-随笔分类-Lua</title><link>http://www.cppblog.com/cslover/category/20726.html</link><description>热爱游戏开发，专注于手游业务</description><language>zh-cn</language><lastBuildDate>Sat, 21 Dec 2013 14:20:47 GMT</lastBuildDate><pubDate>Sat, 21 Dec 2013 14:20:47 GMT</pubDate><ttl>60</ttl><item><title>lua Require函数</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204934.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:42:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204934.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204934.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204934.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204934.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204934.html</trackback:ping><description><![CDATA[<p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;">Lua<span style="font-family: 宋体;">提供高级的</span>require<span style="font-family: 宋体;">函数来加载运行库。粗略的说</span>require<span style="font-family: 宋体;">和</span>dofile<span style="font-family: 宋体;">完成同样的功能但有两点不同：</span></p><p style="margin: 3pt 0cm 4pt 39pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: -18pt; line-height: 20px;">1.<span style="font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;</span>require<span style="font-family: 宋体;">会搜索目录加载文件</span></p><p style="margin: 3pt 0cm 4pt 39pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: -18pt; line-height: 20px;">2.<span style="font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;</span>require<span style="font-family: 宋体;">会判断是否文件已经加载避免重复加载同一文件。由于上述特征，</span>require<span style="font-family: 宋体;">在</span>Lua<span style="font-family: 宋体;">中是加载库的更好的函数。</span></p><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;">require<span style="font-family: 宋体;">使用的路径和普通我们看到的路径还有些区别，我们一般见到的路径都是一个目录列表。</span>require<span style="font-family: 宋体;">的路径是一个模式列表，每一个模式指明一种由虚文件名<br /></span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-family: 宋体;">（</span>require<span style="font-family: 宋体;">的参数）转成实文件名的方法。更明确地说，每一个模式是一个包含可选的问号的文件名。匹配的时候</span>Lua<span style="font-family: 宋体;">会首先将问号用虚文件名替换，然后看是否有<br /></span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-family: 宋体;">这样的文件存在。如果不存在继续用同样的方法用第二个模式匹配。例如，路径如下：</span></p><div style="line-height: 20px; font-size: 12px; font-family: Simsun; border: 1pt dashed windowtext; padding: 1pt 4pt; background-color: #ffffff; margin-left: 21pt; margin-right: 21pt;"><p style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: 'Courier New'; text-align: justify; line-height: 13pt; background-color: #ecf1f4; border: none; padding: 0cm; background-position: initial initial; background-repeat: initial initial;">?;?.lua;c:\windows\?;/usr/local/lua/?/?.lua</p></div><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;"><span style="font-family: 宋体;">调用</span>require "lili"<span style="font-family: 宋体;">时会试着打开这些文件：</span></p><div style="line-height: 20px; font-size: 12px; font-family: Simsun; border: 1pt dashed windowtext; padding: 1pt 4pt; background-color: #ffffff; margin-left: 21pt; margin-right: 21pt;"><p style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: 'Courier New'; text-align: justify; line-height: 13pt; background-color: #f5e1fb; border: none; padding: 0cm; background-position: initial initial; background-repeat: initial initial;">lili</p><p style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: 'Courier New'; text-align: justify; line-height: 13pt; border: none; padding: 0cm;">lili.lua</p><p style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: 'Courier New'; text-align: justify; line-height: 13pt; background-color: #e3f8e4; border: none; padding: 0cm; background-position: initial initial; background-repeat: initial initial;">c:\windows\lili</p><p style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: 'Courier New'; text-align: justify; line-height: 13pt; border: none; padding: 0cm;">/usr/local/lua/lili/lili.lua</p></div><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;">require<span style="font-family: 宋体;">关注的问题只有分号（模式之间的分隔符）和问号，其他的信息（目录分隔符，文件扩展名）在路径中定义。</span></p><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;"><span style="font-family: 宋体;">为了确定路径，</span>Lua<span style="font-family: 宋体;">首先检查全局变量</span>LUA_PATH<span style="font-family: 宋体;">是否为一个字符串，如果是则认为这个串就是路径；否则</span>require<span style="font-family: 宋体;">检查环境变量</span>LUA_PATH<span style="font-family: 宋体;">的值，如果两个都失败<br /></span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;require<span style="font-family: 宋体;">使用固定的路径（典型的</span>"?;?.lua"<span style="font-family: 宋体;">）</span></p><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;">require<span style="font-family: 宋体;">的另一个功能是避免重复加载同一个文件两次。</span>Lua<span style="font-family: 宋体;">保留一张所有已经加载的文件的列表（使用</span>table<span style="font-family: 宋体;">保存）。如果一个加载的文件在表中存在</span>require<span style="font-family: 宋体;">简单的返回；<br /></span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-family: 宋体;">表中保留加载的文件的虚名，而不是实文件名。所以如果你使用不同的虚文件名</span>require<span style="font-family: 宋体;">同一个文件两次，将会加载两次该文件。比如</span>require "foo"<span style="font-family: 宋体;">和</span>require "foo.lua"<span style="font-family: 宋体;">，<br /></span>&nbsp;<span style="font-family: 宋体;">路径为</span>"?;?.lua"<span style="font-family: 宋体;">将会加载</span>foo.lua<span style="font-family: 宋体;">两次。我们也可以通过全局变量</span>_LOADED<span style="font-family: 宋体;">访问文件名列表，这样我们就可以判断文件是否被加载过；同样我们也可以使用一点小技巧让<br /></span>require<span style="font-family: 宋体;">加载一个文件两次。比如，</span>require "foo"<span style="font-family: 宋体;">之后</span>_LOADED["foo"]<span style="font-family: 宋体;">将不为</span>nil<span style="font-family: 宋体;">，我们可以将其赋值为</span>nil<span style="font-family: 宋体;">，</span>require "foo.lua"<span style="font-family: 宋体;">将会再次加载该文件。</span></p><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;"><span style="font-family: 宋体;">一个路径中的模式也可以不包含问号而只是一个固定的路径，比如：</span></p><div style="line-height: 20px; font-size: 12px; font-family: Simsun; border: 1pt dashed windowtext; padding: 1pt 4pt; background-color: #e5ece0; margin-left: 21pt; margin-right: 21pt;"><p style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: 'Courier New'; text-align: justify; line-height: 13pt; background-color: #ffffff; border: none; padding: 0cm; background-position: initial initial; background-repeat: initial initial;">?;?.lua;/usr/local/default.lua</p></div><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;"><span style="font-family: 宋体;">这种情况下，</span>require<span style="font-family: 宋体;">没有匹配的时候就会使用这个固定的文件（当然这个固定的路径必须放在模式列表的最后才有意义）。在</span>require<span style="font-family: 宋体;">运行一个</span>chunk<span style="font-family: 宋体;">以前，它定义了<br />一个全局变量</span>_REQUIREDNAME<span style="font-family: 宋体;">用来保存被</span>required<span style="font-family: 宋体;">的虚文件的文件名。我们可以通过使用这个技巧扩展</span>require<span style="font-family: 宋体;">的功能。举个极端的例子，我们可以把路径设为<br /></span>"/usr/local/lua/newrequire.lua"<span style="font-family: 宋体;">，这样以后每次调用</span>require<span style="font-family: 宋体;">都会运行</span>newrequire.lua<span style="font-family: 宋体;">，这种情况下可以通过使用</span>_REQUIREDNAME<span style="font-family: 宋体;">的值去实际加载</span>required<span style="font-family: 宋体;">的文件。</span></p><img src ="http://www.cppblog.com/cslover/aggbug/204934.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:42 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204934.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> table的函数API</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204932.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:39:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204932.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204932.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204932.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204932.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204932.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转载自http://dhq.me/lua-learning-notes-tabletable 在 Lua 里是一种重要的数据结构，它可以说是其他数据结构的基础，通常的数组、记录、线性表、队列、集合等数据结构都可以用 table 来表示，甚至连全局变量（_G）、模块、元表（metatable）等这些重要的 Lua 元素都是 table 的结构。可以说，table &nbsp;是一个强大而又神奇的东西...&nbsp;&nbsp;<a href='http://www.cppblog.com/cslover/archive/2013/12/21/204932.html'>阅读全文</a><img src ="http://www.cppblog.com/cslover/aggbug/204932.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:39 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204932.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Lua库之时间和日期操作</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204931.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:36:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204931.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204931.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204931.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204931.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204931.html</trackback:ping><description><![CDATA[<span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">转载自</span><a href="http://www.cppblog.com/jsunhj/archive/2008/11/28/68082.html">http://www.cppblog.com/jsunhj/archive/2008/11/28/68082.html</a><br /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;"><br />os.time()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;== 返回当前系统的</span><a href="http://www.cppblog.com/jsunhj/archive/2008/11/28/68084.html" style="color: #770000; text-decoration: none; font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">日历时间</a><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">os.date()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;== 返回本地化的时间字符串，这里是"11/28/08 17:23:37"</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">os.date("%x", os.time()) &lt;== 返回自定义格式化时间字符串（</span><a href="http://www.cppblog.com/jsunhj/admin/EditPosts.aspx#FullArgList" style="color: #770000; text-decoration: none; font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">完整的格式化参数</a><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">），这里是"11/28/08"</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">os.clock()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;== 返回执行该程序CPU花去的时钟秒数，这里是1156.726</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><strong style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;"><span style="font-size: 12pt;">附录</span></strong><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><a href="http://www.cppblog.com/jsunhj/admin/EditPosts.aspx#FullArgList" style="color: #770000; text-decoration: none; font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">完整的格式化参数</a><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">这些时间输出的字符串表示是经过本地化的。所以如果是在巴西（葡萄牙语系），"%B" 得到的就是 "setembro"（译者按：大概是葡萄牙语九月？），"%x" 得到的就是 "16/09/98"（月日次序不同）。标记的意义和显示实例总结如下表。实例的时间是在1998年九月16日，星期三，23:48:10。返回值为数字形式的还列出了它们的范围。（都是按照英语系的显示描述的，也比较简单，就不烦了）</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%a abbreviated weekday name (e.g., Wed)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%A full weekday name (e.g., Wednesday)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%b abbreviated month name (e.g., Sep)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%B full month name (e.g., September)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;"><strong>%c</strong>&nbsp;date and time (e.g., 09/16/98 23:48:10)&nbsp;<br /></span><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%d day of the month (16) [01-31]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%H hour, using a 24-hour clock (23) [00-23]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%I hour, using a 12-hour clock (11) [01-12]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%M minute (48) [00-59]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%m month (09) [01-12]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%p either "am" or "pm" (pm)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%S second (10) [00-61]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%w weekday (3) [0-6 = Sunday-Saturday]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%x date (e.g., 09/16/98)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%X time (e.g., 23:48:10)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%Y full year (1998)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%y two-digit year (98) [00-99]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%% the character '%'</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" />事实上如果不使用任何参数就调用date，就是以%c的形式输出。这样就是得到经过格式化的完整时间信息。<br /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">注意%x、%X和%c由所在地区和计算机系统的改变会发生变化。</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">如果该字符串要确定下来（例如确定为mm/dd/yyyy），可以使用明确的字符串格式方式（例如"%m/%d/%Y"）。</span><img src ="http://www.cppblog.com/cslover/aggbug/204931.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:36 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204931.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Lua module机制分析</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204928.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:25:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204928.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204928.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204928.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204928.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204928.html</trackback:ping><description><![CDATA[<span style="margin: 0px; padding: 0px; font-size: small; color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; color: #000000; font-size: 13px; line-height: 19px;"><span style="margin: 0px; padding: 0px;">转载自<a href="http://www.cnblogs.com/orez88/articles/2139160.html">http://www.cnblogs.com/orez88/articles/2139160.html</a><br />Lua</span><span style="margin: 0px; padding: 0px;">中的模块</span><span style="margin: 0px; padding: 0px;">(module)</span><span style="margin: 0px; padding: 0px;">机制有点类似于</span><span style="margin: 0px; padding: 0px;">C</span><span style="margin: 0px; padding: 0px;">语言的动态库，可以将一些公共的代码放在模块中，以</span><span style="margin: 0px; padding: 0px;">API</span><span style="margin: 0px; padding: 0px;">的形式提供给</span><span style="margin: 0px; padding: 0px;">Lua</span><span style="margin: 0px; padding: 0px;">应用脚本使用。<br />使用模块或许还可以做成像</span><span style="margin: 0px; padding: 0px;">Android</span><span style="margin: 0px; padding: 0px;">的</span><span style="margin: 0px; padding: 0px;">Application Frameworks</span><span style="margin: 0px; padding: 0px;">，建立一种新的应用程序编程模型，只是猜想。</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; color: #000000; font-size: 13px; line-height: 19px;"><span style="margin: 0px; padding: 0px;">模块最简单的使用方式：</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; color: #000000; font-size: 13px; line-height: 19px;"></p><div bg_javascript"="" style="margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; font-family: Consolas, 'Courier New', Courier, mono; background-color: transparent; width: 687px; word-break: break-all; word-wrap: break-word;"><div style="margin: 0px;"><div style="margin: 0px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; font-weight: bold; color: black;"><a href="http://blog.csdn.net/chenyufei1013/article/details/4438801#" title="view plain" style="padding: 1px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); border-bottom-style: dotted; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;">view plain</a></div></div><ol start="1" style="margin-left: 0px; padding-top: 2px; padding-bottom: 2px; padding-left: 50px; border-style: solid; color: #2b91af;"><li style="margin-left: 38px; list-style-type: decimal; line-height: 18px;">require&nbsp;&#8220;os&#8221;&nbsp;&nbsp;</li><li style="margin-left: 38px; list-style-type: decimal; background-color: #fafafa; line-height: 18px;">os.clock() &nbsp;</li></ol></div></span><ul style="margin: 0px 0px 0px 45px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px; list-style: inherit !important;"><strong style="margin: 0px; padding: 0px;">table</strong></li></ul><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">Lua</span><span style="margin: 0px; padding: 0px;">语言共有</span><span style="margin: 0px; padding: 0px;">8</span><span style="margin: 0px; padding: 0px;">中数据类型，其中</span><span style="margin: 0px; padding: 0px;">table(</span><span style="margin: 0px; padding: 0px;">表</span><span style="margin: 0px; padding: 0px;">)</span><span style="margin: 0px; padding: 0px;">类型实现了&#8220;关联数组（</span><span style="margin: 0px; padding: 0px;">associative array</span><span style="margin: 0px; padding: 0px;">）&#8221;。</span><span style="margin: 0px; padding: 0px;">table</span><span style="margin: 0px; padding: 0px;">中可以保存各种数据类型。和</span><span style="margin: 0px; padding: 0px;">table</span><span style="margin: 0px; padding: 0px;">关联的<a name="2.8" style="margin: 0px; padding: 0px; color: rgb(51, 102, 153); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51); width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat;"></a></span><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;">metatable</span></span><span style="margin: 0px; padding: 0px;">，<br />它定义了表的操作的一些行为，比如</span><span style="margin: 0px; padding: 0px;">:&nbsp;</span><span style="margin: 0px; padding: 0px;">使用</span><span style="margin: 0px; padding: 0px;">+</span><span style="margin: 0px; padding: 0px;">操作时，会去查找</span><span style="margin: 0px; padding: 0px;">metatable</span><span style="margin: 0px; padding: 0px;">的属性</span><span style="margin: 0px; padding: 0px;">__add</span><span style="margin: 0px; padding: 0px;">，有的话执行。</span></p><ul style="margin: 0px 0px 0px 45px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px; list-style: inherit !important;"><strong style="margin: 0px; padding: 0px;">function</strong></li></ul><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">function</span><span style="margin: 0px; padding: 0px;">是</span><span style="margin: 0px; padding: 0px;">Lua</span><span style="margin: 0px; padding: 0px;">中</span><span style="margin: 0px; padding: 0px;">8</span><span style="margin: 0px; padding: 0px;">中基本类型之一，和其它类型一样可以作为值传递。和</span><span style="margin: 0px; padding: 0px;">function</span><span style="margin: 0px; padding: 0px;">有关联的表是环境表（</span><em style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;">environment</span></em><span style="margin: 0px; padding: 0px;">），可以认为是</span><span style="margin: 0px; padding: 0px;">function</span><span style="margin: 0px; padding: 0px;">的执行环境。<br />可以通过</span><span style="margin: 0px; padding: 0px;">setfenv()</span><span style="margin: 0px; padding: 0px;">和</span><span style="margin: 0px; padding: 0px;">getfenv()</span><span style="margin: 0px; padding: 0px;">来设置和获取函数的环境表。函数中对于全局的操作，比如：定义全局变量等；就是在这个表中进行的。多个函数可以共享一个表</span></p><ul style="margin: 0px 0px 0px 45px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px; list-style: inherit !important;"><strong style="margin: 0px; padding: 0px;">require</strong></li></ul><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">当使用</span><span style="margin: 0px; padding: 0px;">require</span><span style="margin: 0px; padding: 0px;">加载一个模块时，实际上做了如下的事情：</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;"></span><span style="margin: 0px; padding: 0px;">在表</span><span style="margin: 0px; padding: 0px;">package.loaded</span><span style="margin: 0px; padding: 0px;">中查找模块是否存在，有的话就返回该值。可以使用如下函数输出表中的内容</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"></p><div bg_c-sharp"="" style="margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; font-family: Consolas, 'Courier New', Courier, mono; background-color: #ffffff; width: 687px; word-break: break-all; word-wrap: break-word;"><div style="margin: 0px;"><div style="margin: 0px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; font-weight: bold;"><a href="http://blog.csdn.net/chenyufei1013/article/details/4438801#" title="view plain" style="padding: 1px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); border-bottom-style: dotted; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;">view plain</a></div></div><ol start="1" style="margin-left: 0px; padding-top: 2px; padding-bottom: 2px; padding-left: 50px; border-style: solid; color: #2b91af;"><li style="margin-left: 38px; list-style-type: decimal; line-height: 18px;">function&nbsp;walkTable(t)&nbsp;&nbsp;</li><li style="margin-left: 38px; list-style-type: decimal; background-color: #fafafa; line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055;">for</span>&nbsp;k,v&nbsp;<span style="color: #7f0055;">in</span>&nbsp;pairs(t)&nbsp;<span style="color: #7f0055;">do</span>&nbsp;&nbsp;</li><li style="margin-left: 38px; list-style-type: decimal; line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("+++",k,v,"+++")&nbsp;&nbsp;</li><li style="margin-left: 38px; list-style-type: decimal; background-color: #fafafa; line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;&nbsp;</li><li style="margin-left: 38px; list-style-type: decimal; line-height: 18px;">end &nbsp;</li></ol></div><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">package.loaded</span><span style="margin: 0px; padding: 0px;">中的内容如下，其中包含了一些默认加载的库和全局表</span><span style="margin: 0px; padding: 0px;">_G</span><br /></p><div>+++ string &nbsp; &nbsp; &nbsp;, &nbsp; &nbsp; &nbsp;table: 003B82B0 &nbsp; &nbsp; +++ &nbsp;</div><div>+++ debug &nbsp; &nbsp; &nbsp;, &nbsp; &nbsp; &nbsp;table: 003B9300 &nbsp; &nbsp; &nbsp;+++ &nbsp;</div><div>+++ package &nbsp; , &nbsp; &nbsp; &nbsp;table: 003B5C30 &nbsp; &nbsp; +++ &nbsp;</div><div>+++ _G &nbsp; , &nbsp; &nbsp; &nbsp;table: 003B2E10 &nbsp; &nbsp; +++ &nbsp;</div><div>+++ io &nbsp; &nbsp; , &nbsp; &nbsp; &nbsp;table: 003B7320 &nbsp; &nbsp; &nbsp;+++ &nbsp;</div><div>+++ os &nbsp; &nbsp;, &nbsp; &nbsp; &nbsp;table: 003B7988 &nbsp; &nbsp; &nbsp;+++ &nbsp;</div><div>+++ table , &nbsp; &nbsp; &nbsp;table: 003B51E0 &nbsp; &nbsp; +++ &nbsp;</div><div>+++ math &nbsp; &nbsp; &nbsp; , &nbsp; &nbsp; &nbsp;table: 003B8728 &nbsp; &nbsp; &nbsp;+++ &nbsp;</div><div>+++ coroutine , &nbsp; &nbsp; &nbsp;table: 003B59D8 &nbsp; &nbsp; +++ &nbsp;</div><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;"><br />为模块查找加载器（</span><span style="margin: 0px; padding: 0px;">loader</span><span style="margin: 0px; padding: 0px;">），查询</span><span style="margin: 0px; padding: 0px;">package.preload</span><span style="margin: 0px; padding: 0px;">，通常为空</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">加载</span><span style="margin: 0px; padding: 0px;">Lua</span><span style="margin: 0px; padding: 0px;">文件使用</span><span style="margin: 0px; padding: 0px;">loadfile</span><span style="margin: 0px; padding: 0px;">函数。</span><span style="margin: 0px; padding: 0px;">loadfile</span><span style="margin: 0px; padding: 0px;">会将文件当作函数来加载，</span><span style="margin: 0px; padding: 0px;">require</span><span style="margin: 0px; padding: 0px;">会将模块名作为参数传给该函数。若有返回值则将返回值放入表</span><span style="margin: 0px; padding: 0px;">package.loaded</span><span style="margin: 0px; padding: 0px;">中。<br />若没有则返回表</span><span style="margin: 0px; padding: 0px;">package.loaded</span><span style="margin: 0px; padding: 0px;">中的值。</span></p><ul style="margin: 0px 0px 0px 45px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px; list-style: inherit !important;"><strong style="margin: 0px; padding: 0px;">module</strong></li></ul><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">当在模块文件中使用</span><span style="margin: 0px; padding: 0px;">module</span><span style="margin: 0px; padding: 0px;">函数的时候，如下所示；</span></p><div bg_c-sharp"="" style="margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; font-family: Consolas, 'Courier New', Courier, mono; background-color: #ffffff; width: 687px; word-break: break-all; word-wrap: break-word;"><ol start="1" style="margin-left: 0px; padding-top: 2px; padding-bottom: 2px; padding-left: 50px; border-style: solid; color: #2b91af;"><li style="margin-left: 38px; list-style-type: decimal; line-height: 18px;">module&nbsp;&#8220;mymodule&#8221; &nbsp;</li></ol></div><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">实际上等同于以下的语句：</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"></p><div bg_c-sharp"="" style="margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; font-family: Consolas, 'Courier New', Courier, mono; background-color: #ffffff; width: 687px; word-break: break-all; word-wrap: break-word;"><div style="margin: 0px;"><div style="margin: 0px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; font-weight: bold;"><div>local modname = &#8220;mymodule&#8221; &nbsp; &nbsp; &#8211; 定义模块名 &nbsp;</div><div>local M = {} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- 定义用于返回的模块表 &nbsp;</div><div>_G[modname] = M &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- 将模块表加入到全局变量中 &nbsp;</div><div>package.loaded[modname] = M &nbsp; &nbsp;-- 将模块表加入到package.loaded中，防止多次加载 &nbsp;</div><div>setfenv(1,M) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- 将模块表设置为函数的环境表，这使得模块中的所有操作是以在模块表中的，这样定义函数就直接定义在模块表中&nbsp;</div></div></div></div>&nbsp;<span style="margin: 0px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;">通过</span><span style="margin: 0px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;">module()</span><span style="margin: 0px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;">，可以方便的编写模块中的内容。</span><img src ="http://www.cppblog.com/cslover/aggbug/204928.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:25 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204928.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>解析LUA中关于Package定义</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204925.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:14:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204925.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204925.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204925.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204925.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204925.html</trackback:ping><description><![CDATA[<p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;"><strong>转载自</strong><a href="http://mobile.51cto.com/iphone-285706.htm">http://mobile.51cto.com/iphone-285706.htm</a><br /><strong>LUA</strong>中关于<strong>Package</strong>定义学习笔记是本文要介绍的内容，主要是来学习<strong>lua</strong>中对<strong>Package</strong>，具体内容俩看本文详解。<strong>Package</strong>是个很逻辑的概念。也就是说很可能不是语言直接提供的设施。cpp里的namespace可以用来实现<strong>package</strong>，在<strong>LUA</strong>中则是依靠table机制来实现。实现的方式大同小异，本文总结了在《<strong>lua</strong>中文手册》中出现的3种方式。</p><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">方式一: 直接将所有组成包的数据和方法定义在包中，代码看起来像是这样：</p><pre style="margin-top: 0px; padding: 0px; color: #333333; font-family: 'Courier New', monospace; font-size: 12px; width: 591.015625px; overflow: auto; background-color: #e6e6e6; line-height: 28px;"><ol style="margin-left: 55px; padding: 5px 0px; color: #5c5c5c; list-style-position: initial; list-style-image: initial; word-wrap: break-word; word-break: normal; border: none; background-color: #f7f7f7; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important;"><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">vector3d</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;{}&nbsp;&nbsp;--&nbsp;包名 &nbsp;</span></span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">function&nbsp;vector3d.function1() &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">...... &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">end &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">function&nbsp;vector3d.function2() &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">...... &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(vector3d.function1())&nbsp;then &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...... &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">end &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">return&nbsp;vector3d&nbsp;</span></li></ol></pre><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">这样定义的就是一个vector3d包，使用require语言打开这个包后，就可以使用 vector3d.function1和vector3d.function2这两个函数了。</p><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">这是最直接最好理解的一种Package定义方式，但是有一定的弊端。这个弊端主要体现在Package的实现过程中。可以看到，即使在</p><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">vector3d.function2()中使用function1()函数，也必须完整的加上vector3d包名，否则无法进行函数调用。Package的作者要稍微累一点，不过使用者倒是还好。特别的注意最后的 return vector3d 语句，有了这句后调用者可以按照如下方式重命名包:</p><pre style="margin-top: 0px; padding: 0px; color: #333333; font-family: 'Courier New', monospace; font-size: 12px; width: 591.015625px; overflow: auto; background-color: #e6e6e6; line-height: 28px;"><ol style="margin-left: 55px; padding: 5px 0px; color: #5c5c5c; list-style-position: initial; list-style-image: initial; word-wrap: break-word; word-break: normal; border: none; background-color: #f7f7f7; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important;"><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">MyPackage</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;&nbsp;</span><span style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">require</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;"vector3d" &nbsp;</span></span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">MyPackage.function2()&nbsp;</span></li></ol></pre><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">方式二: 使用局部函数定义所有的<strong>Package</strong>内函数，然后在Package的结尾处将需要公开的函数直接放入Package中。代码看起来像这样:</p><pre style="margin-top: 0px; padding: 0px; color: #333333; font-family: 'Courier New', monospace; font-size: 12px; width: 591.015625px; overflow: auto; background-color: #e6e6e6; line-height: 28px;"><ol style="margin-left: 55px; padding: 5px 0px; color: #5c5c5c; list-style-position: initial; list-style-image: initial; word-wrap: break-word; word-break: normal; border: none; background-color: #f7f7f7; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important;"><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">vector3d</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;{}&nbsp;&nbsp;--&nbsp;包名 &nbsp;</span></span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">local&nbsp;function&nbsp;function1() &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">...... &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">end &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;">&nbsp;</li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">local&nbsp;function&nbsp;function2() &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">...... &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(function1())&nbsp;then &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...... &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">end &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">vector3d</span><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;=&nbsp;{</span><span style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">function1</span><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;=&nbsp;</span><span style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">functoin1</span><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">,&nbsp; &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">function2</span><span style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">function2</span><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;=&nbsp;function2 &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">return&nbsp;vector3d&nbsp;</span></li></ol></pre><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">最后给包中赋值的部分就是将需要的接口公开的部分。这样做的好处：不需要公开的函数可以完全隐藏起来(都是local函数)；Package内部的各个函数相互之间调用的时候不再需要加Package名称进行区分; 可以按照需要随意的重命名Package公开的接口名称。这种方式的弊端在于定义的时候需要写上local，这算不算弊端就看你了 - -。 就我个人而言，我还是比较喜欢这个定义方式。能明确的区分出接口和私有的定义，公开接口的名称还可以随意改变，这就意味着可以随意替换内部实现而不需要影响外部调用者。</p><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">无论用什么方法去定义Package，都是为了在逻辑上更好的规划代码层次。<strong>LUA</strong>中的table机制的确是一个活力无限的机制啊。Package依靠这个实现，<strong>LUA</strong>本身自己有些机制也依赖于Table(比如全局变量就放在_G表中)</p><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">手册里还有个如何"拆开"<strong>Package</strong>的代码段，很好的展现了table的强大之处(Package也是在table上构筑的逻辑产物)。将Package拆开的意思，就是将所有 Package中公开的名字放入_G表中。也就是让 Package.A() 变成_G.A&nbsp; (_G在一般情况下不需要写，默认引用了)</p><pre style="margin-top: 0px; padding: 0px; color: #333333; font-family: 'Courier New', monospace; font-size: 12px; width: 591.015625px; overflow: auto; background-color: #e6e6e6; line-height: 28px;"><ol style="margin-left: 55px; padding: 5px 0px; color: #5c5c5c; list-style-position: initial; list-style-image: initial; word-wrap: break-word; word-break: normal; border: none; background-color: #f7f7f7; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important;"><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">function&nbsp;openpackage&nbsp;(ns) &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;n,v&nbsp;in&nbsp;pairs(ns)&nbsp; &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_G[n]&nbsp;=&nbsp;v &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">end&nbsp;<br /></span></li></ol></pre><img src ="http://www.cppblog.com/cslover/aggbug/204925.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:14 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204925.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Lua中xpcall函数</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204924.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:03:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204924.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204924.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204924.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204924.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204924.html</trackback:ping><description><![CDATA[<span style="background-color: #ffffff; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px;">Lua提供了xpcall来实现这个功能，xpcall接受两个参数：调用函数、错误处理函数。当错误发生时，Lua会在栈释放以前调用错误处理函数，因此可以使用debug库收集错误相关信息。<br />有两个常用的debug处理函数：debug.debug和debug.traceback，前者给出Lua的提示符，你可以自己动手察看错误发生时的情况；后者通过traceback创建更多的错误信息，也是控制台解释器用来构建错误信息的函数。<br />你可以在任何时候调用debug.traceback获取当前运行的traceback信息：</span><br style="background-color: #ffffff; padding: 0px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px;" /><span style="background-color: #ffffff; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px;">print(debug.traceback())<br /></span><img src ="http://www.cppblog.com/cslover/aggbug/204924.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:03 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204924.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Lua中的字符串函数库</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204923.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:00:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204923.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204923.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204923.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204923.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204923.html</trackback:ping><description><![CDATA[<div><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 20px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; color: #ffcc00; font-family: Courier;">Lua解释器对字符串的支持很有限。一个程序可以创建字符串并连接字符串，但不能截取子串，检查字符串的大小，检测字符串的内容。在Lua中操纵字符串的功能基本来自于string库。<br style="margin: 0px; padding: 0px;" /></span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; font-family: Arial;">字符串库中的一些函数是非常简单的：<br style="margin: 0px; padding: 0px;" /></span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00; font-family: Arial;">string.</span><span style="margin: 0px; padding: 0px; color: #99cc00; font-family: Arial;">len</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(s)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 返回字符串s的长度；</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00; font-family: Arial;">string.</span><span style="margin: 0px; padding: 0px; color: #99cc00; font-family: Arial;">rep</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(s, n)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;返回重复n次字符串s的串；你使用</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.rep</span><span style="margin: 0px; padding: 0px; font-family: Arial;">("a", 2^20)可以创建一个1M bytes的字符串（比如，为了测试需要）；</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00; font-family: Arial;">string.</span><span style="margin: 0px; padding: 0px; color: #99cc00; font-family: Arial;">lower</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(s)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 将s中的大写字母转换成小写（</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.upper</span><span style="margin: 0px; padding: 0px; font-family: Arial;">将小写转换成大写）。如果你想不关心大小写对一个数组进行排序的话，你可以这样：</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00; font-family: Arial;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="margin: 0px; padding: 0px; font-family: Arial;">table.sort</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(a, function (a, b)&nbsp;</span><span style="margin: 0px; padding: 0px; font-family: Arial;">return&nbsp;</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.lower</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(a) &lt;&nbsp;</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.lower</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(b)&nbsp;</span><span style="margin: 0px; padding: 0px; font-family: Arial;">end)</span><span style="margin: 0px; padding: 0px; font-family: Arial;"><br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; color: #99cc00; font-family: Arial;">string.upper</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(s)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 将s中的小写字母转换成大写</span><span style="margin: 0px; padding: 0px; color: #99cc00; font-family: Courier;"><br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; color: #99cc00; font-family: Arial;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.upper</span><span style="margin: 0px; padding: 0px; font-family: Arial;">和</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.lower</span><span style="margin: 0px; padding: 0px; font-family: Arial;">都依赖于本地环境变量。所以，如果你在 European Latin-1环境下，表达式：</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; font-family: Arial;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string.upper</span><span style="margin: 0px; padding: 0px; font-family: Arial;">("a??o")&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="margin: 0px; padding: 0px; font-family: Arial;">--&gt; "A??O"</span><span style="margin: 0px; padding: 0px; font-family: Arial;"><br style="margin: 0px; padding: 0px; color: #99cc00;" /></span><span style="margin: 0px; padding: 0px; color: #99cc00; font-family: Arial;">string.<span style="margin: 0px; padding: 0px;">sub</span><span style="margin: 0px; padding: 0px;">(s,i,j)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="margin: 0px; padding: 0px; font-family: Arial;">函数截取字符串s的从第i个字符到第j个字符之间的串。Lua中，字符串的第一个字符索引从1开始。你也可以使用负索引，负索引从字符串的结尾向前计数：-1指向最后一个字符，-2指向倒数第二个，以此类推。所以，&nbsp;</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.sub</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(s, 1, j)返回字符串s的长度为j的前缀；</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.sub</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(s, j, -1)返回从第j个字符开始的后缀。如果不提供第3个参数，默认为-1，因此我们将最后一个调用写为</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.sub</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(s, j)；</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.sub</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(s, 2, -2)返回去除第一个和最后一个字符后的子串。</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">s = "[in brackets]"</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">print(</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">string.sub</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">(s, 2, -2)) --&gt; in brackets</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; font-family: Arial;">记住：Lua中的字符串是恒定不变的。</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.sub</span><span style="margin: 0px; padding: 0px; font-family: Arial;">函数以及Lua中其他的字符串操作函数都不会改变字符串的值，而是返回一个新的字符串。一个常见的错误是：</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; font-family: Arial;">string.sub</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(s, 2, -2)</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; font-family: Arial;">认为上面的这个函数会改变字符串s的值。如果你想修改一个字符串变量的值，你必须将变量赋给一个新的字符串：</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; font-family: Arial;">s =&nbsp;</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.sub</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(s, 2, -2)</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; font-family: Arial;">string.</span><span style="margin: 0px; padding: 0px; font-family: Arial;">char</span><span style="margin: 0px; padding: 0px; font-family: Arial;">函数和</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.</span><span style="margin: 0px; padding: 0px; font-family: Arial;">byte</span><span style="margin: 0px; padding: 0px; font-family: Arial;">函数用来将字符在字符和数字之间转换。string.char获取0个或多个整数，将每一个数字转换成字符，然后返回一个所有这些字符连接起来的字符串。</span><span style="margin: 0px; padding: 0px; font-family: Arial;">string.byte</span><span style="margin: 0px; padding: 0px; font-family: Arial;">(s, i)将字符串s的第i个字符的转换成整数；第二个参数是可选的，缺省情况下i=1。下面的例子中，我们假定字符用ASCII表示：<br style="margin: 0px; padding: 0px;" /></span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">print(</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">string.char</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">(97)) --&gt; a</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">i = 99; print(</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">string.char</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">(i, i+1, i+2)) --&gt; cde</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">print(</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">string.byte</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">("abc")) --&gt; 97</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">print(</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">string.byte</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">("abc", 2)) --&gt; 98</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">print(</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">string.byte</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">("abc", -1)) --&gt; 99&nbsp;</span><span style="margin: 0px; padding: 0px; font-family: Courier;"><br style="margin: 0px; padding: 0px;" /><br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; font-family: Courier;">上面最后一行，我们使用负数索引访问字符串的最后一个字符。<br style="margin: 0px; padding: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 20px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px;">Lua提供了</span><span style="margin: 0px; padding: 0px; color: #99cc00;">string.format</span><span style="margin: 0px; padding: 0px;">()函数来生成具有特定格式的字符串, 函数的第一个参数是格式(formatstring), 之后是对应格式中每个代号的各种数据. 由于格式字符串的存在, 使得产生的长字符串可读性大大提高了. 这个函数的格式很像C语言中的printf().<span style="margin: 0px; padding: 0px; font-family: Arial;">函数</span><span style="margin: 0px; padding: 0px; color: #99cc00; font-family: Arial;">string.format</span><span style="margin: 0px; padding: 0px; font-family: Arial;">在用来对字符串进行格式化的时候，特别是字符串输出，是功能强大的工具。这个函数有两个参数，你完全可以照C语言的</span><span style="margin: 0px; padding: 0px; font-family: Arial;">printf</span><span style="margin: 0px; padding: 0px; font-family: Arial;">来使用这个函数。第一个参数为格式化串：由指示符和控制格式的字符组成。指示符后的控制格式的字符可以为：十进制'd'；十六进制'x'；八进制'o'；浮点数'f'；字符串's'。在指示符'%'和控制格式字符之间还可以有其他的选项：用来控制更详细的格式，比如一个浮点数的小数的位数：</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 20px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px;">格式字符串可能包含以下的转义码:</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 20px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; color: #99cc00;">%c - 接受一个数字, 并将其转化为ASCII码表中对应的字符</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00;">%d, %i - 接受一个数字并将其转化为有符号的整数格式</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00;">%o - 接受一个数字并将其转化为八进制数格式</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00;">%u - 接受一个数字并将其转化为无符号整数格式</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00;">%x - 接受一个数字并将其转化为十六进制数格式, 使用小写字母</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00;">%X - 接受一个数字并将其转化为十六进制数格式, 使用大写字母</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00;">%e - 接受一个数字并将其转化为科学记数法格式, 使用小写字母e</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00;">%E - 接受一个数字并将其转化为科学记数法格式, 使用大写字母E</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00;">%f - 接受一个数字并将其转化为浮点数格式</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00;">%g(%G) - 接受一个数字并将其转化为%e(%E, 对应%G)及%f中较短的一种格式</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00;">%q - 接受一个字符串并将其转化为可安全被Lua编译器读入的格式</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #99cc00;">%s - 接受一个字符串并按照给定的参数格式化该字符串</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 20px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px;">为进一步细化格式, 可以在%号后添加参数. 参数将以如下的顺序读入:</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 20px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px;">(1) 符号: 一个+号表示其后的数字转义符将让正数显示正号. 默认情况下只有负数显示符号.</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px;">(2) 占位符: 一个0, 在后面指定了字串宽度时占位用. 不填时的默认占位符是空格.</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px;">(3) 对齐标识: 在指定了字串宽度时, 默认为右对齐, 增加-号可以改为左对齐.</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px;">(4) 宽度数值</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px;">(5) 小数位数/字串裁切: 在宽度数值后增加的小数部分n, 若后接f(浮点数转义符, 如%6.3f)则设定该浮点数的小数只保留n位, 若后接s(字符串转义符, 如%5.3s)则设定该字符串只显示前n位.</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 20px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px;">在这些参数的后面则是上述所列的转义码类型(c, d, i, f, ...).<br style="margin: 0px; padding: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 20px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px;"><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">print(</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">string.format</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">("pi = %.4f", PI))<br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --&gt; pi = 3.1416<br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">d = 5; m = 11; y = 1990<br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">print(</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">string.format</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">("%02d/%02d/%04d", d, m, y))<br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --&gt; 05/11/1990<br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">tag, title = "h1", "a title"<br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">print(</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">string.format</span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">("&lt;%s&gt;%s&lt;/%s&gt;", tag, title, tag))<br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; color: #00ccff; font-family: Courier;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --&gt; &lt;h1&gt;a title&lt;/h1&gt;<br style="margin: 0px; padding: 0px;" /><br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px; font-family: Courier;">第一个例子，%.4f代表小数点后面有4位小数的浮点数。第二个例子%02d代表以固定的两位显示十进制数，不足的前面补0。而%2d前面没有指定0，不足两位时会以空白补足。对于格式串部分指示符得详细描述清参考lua手册，或者参考C手册，因为Lua调用标准C的printf函数来实现最终的功能。</span><br style="margin: 0px; padding: 0px;" /><br style="margin: 0px; padding: 0px;" /></span><span style="margin: 0px; padding: 0px;">以下是一些例子:</span><span style="margin: 0px; padding: 0px; color: #00ccff;"><br style="margin: 0px; padding: 0px;" /><br style="margin: 0px; padding: 0px;" /><br style="margin: 0px; padding: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; line-height: 20px; background-color: #ffffff; font-family: Courier;"><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%%c: %c", 83)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输出S</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%+d", 17.0)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输出+17</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%05d", 17)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 输出00017</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%o", 17)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输出21</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%u", 3.14)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 输出3</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%x", 13)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输出d</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%X", 13)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输出D</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%e", 1000)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 输出1.000000e+03</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%E", 1000)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输出1.000000E+03</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%6.3f", 13)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 输出13.000</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%q", "One\nTwo")&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输出"One\</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　　Two"</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%s", "monkey")&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输出monkey</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%10s", "monkey")&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输出&nbsp;&nbsp;&nbsp; monkey</span><br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: #00ccff;">string.format("%5.3s", "monkey")&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 输出&nbsp; mon<br /><br />转载自&nbsp;<a href="http://www.cnblogs.com/whiteyun/archive/2009/08/07/1540899.html">http://www.cnblogs.com/whiteyun/archive/2009/08/07/1540899.html</a><br /><br /><br /></span></p></div><img src ="http://www.cppblog.com/cslover/aggbug/204923.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:00 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204923.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>