Lua1.1 Lua 的参考手册 (三)

(接上篇)

--------------------------------------
7 一些例子
--------------------------------------
本段给出一些显示 Lua 特性的例子。它并不打算覆盖完整的语言,只是显示一有趣的使用。

-------------------
7.1 函数 next 和 nextvar
-------------------
这个例子显示如何使用函数 next 去遍历一个表的字段:
function f (t) -- t is a table
  local i, v = next(t, nil) -- i is an index of t, v = t[i]
  while i do
    -- do something with i and v
    i, v = next(t, i) -- get next index
  end
end

这个例子打印所有值非空的全局变量的名字
function printGlobalVariables ()
  local i, v = nextvar(nil)
  while i do
    print(i)
    i, v = nextvar(i)
  end
end

-------------------
7.2 字符串操作
-------------------
这个例子去掉字符串前后的空白:
function trim(s)
  local i = 1
  while strsub(s,i,i) = ‘ ‘ do
    i = i+1
  end
  local l = strlen(s)
  while strsub(s,l,l) = ‘ ‘ do
    l = l-1
  end
  return strsub(s,i,l)
end

这个例子去掉字符串所有的空白:
function remove_blanks (s)
  local b = strfind(s, ‘ ‘)
  while b do
    s = strsub(s, 1, b-1) .. strsub(s, b+1)
    b = strfind(s, ‘ ‘)
  end
  return s
end

-------------------
7.3 持久化
-------------------
由于 Lua 的自反性,持久化在 Lua 中可以用 Lua 实现。本节展示一些方法来存储和恢复 Lua 中的值,用 Lua 写成的文本文件作为存储媒介。

保存一个键值对,用下面的代码就可以了:
function store (name, value)
  write(‘\n‘ .. name .. ‘=‘)
  write_value(value)
end

function write_value (value)
  local t = type(value)
        if t = ‘nil‘ then write(‘nil‘)
  elseif t = ‘number‘ then write(value)
  elseif t = ‘string‘ then write(‘"‘ .. value .. ‘"‘)
  end
end

为了恢复这些值,一个 lua_dofile 就足够了。

存储表有点复杂。假定表是一棵树,所有下标均为标识符(也就是说,表被用作记录),表的值可以用表的构造函数写成。
首先,把函数 write_value 改为
function write_value (value)
  local t = type(value)
        if t = ‘nil‘ then write(‘nil‘)
  elseif t = ‘number‘ then write(value)
  elseif t = ‘string‘ then write(‘"‘ .. value .. ‘"‘)
  elseif t = ‘table‘ then write_record(value)
  end
end

函数 write_record 是:
function write_record(t)
  local i, v = next(t, nil)
  write(‘@{‘) -- starts constructor
  while i do
    store(i, v)
    i, v = next(t, i)
    if i then write(‘, ‘) end
  end
  write(‘}‘) -- closes constructor
end

-------------------
7.4 一个 Cfunction
-------------------
一个 Cfunction 用来计算最大的数字参数可以写成:
void math_max (void)
{
  int i=1; /* number of arguments */
  double d, dmax;
  lua_Object o;
  /* the function must get at least one argument */
  if ((o = lua_getparam(i++)) == 0)
  { lua_error ("too few arguments to function `max‘"); return; }
  /* and this argument must be a number */
  if (!lua_isnumber(o))
  { lua_error ("incorrect arguments to function `max‘"); return; }
    dmax = lua_getnumber (o);
  /* loops until there is no more arguments */
  while ((o = lua_getparam(i++)) != 0)
  {
    if (!lua_isnumber(o))
    { lua_error ("incorrect arguments to function `max‘"); return; }
    d = lua_getnumber (o);
    if (d > dmax) dmax = d;
  }
  /* push the result to be returned */
  lua_pushnumber (dmax);
}

使用下面的函数注册:
lua_register ("max", math_max);

这个函数就可以由 Lua 调用了,如下:
i = max(4, 5, 10, -34) -- i receives 10

-------------------
7.5 调用 Lua 函数
-------------------
这个例子显示一个 C 函数如何调用一个 7.2节中展示的 Lua 函数 remove_blanks。
void remove_blanks (char *s)
{
  lua_pushstring(s); /* prepare parameter */
  lua_call("remove_blanks", 1); /* call Lua function with 1 parameter */
  strcpy(s, lua_getstring(lua_pop())); /* copy result back to ‘s‘ */
}

--------------------------------------
鸣谢
--------------------------------------
作者要感谢 CENPES/PETROBROBAS 和 TeCGraf 一起,使用该系统的早期版本,并提出宝贵意见。作者还要感谢 Carlos Henrique Levy,为这个语言起了个名字。
---------
-------------------
--------------------------------------

时间: 2024-07-31 23:23:06

Lua1.1 Lua 的参考手册 (三)的相关文章

Ogre参考手册(三)3.1.3 纹理单元TextureUnit

3.1.3 Texture Units 纹理单元 纹理单元通过.material脚本中的texture_unit段设定 texture_alias 设置纹理单元别名,例: texture_unit Diffuse //纹理单元名称 { texture_alias           DiffuseMap //别名 tex_address_mode  clamp } texture 设置纹理层使用的静态纹理图 格式:texture <texturefile> [<type>[unli

lua参考手册01—

2 - 语言 这一节从词法.语法.句法上描述 Lua . 换句话说,这一节描述了哪些 token (符记)是有效的,它们如何被组合起来,这些组合方式有什么含义. 关于语言的构成概念将用常见的扩展 BNF 表达式写出.也就是这个样子: {a} 意思是 0 或多个 a , [a] 意思是一个可选的 a . 非最终的符号会保留原来的样子,关键字则看起来像这样 kword , 其它最终的符号则写成 `=´ . 完整的 Lua 语法可以在本手册最后找到. 2.1 - 词法约定 Lua 中用到的 名字(也称

Lua 5.1 参考手册

Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes 云风 译 www.codingnow.com Copyright © 2006 Lua.org, PUC-Rio. All rights reserved. 1 - 介绍 Lua 是一个扩展式程序设计语言,它被设计成支持通用的过程式编程,并有相关数据描述的设施. Lua 也能对面向对象编程,函数式编程,数据驱动式编程提供很好的支持.

lua参考手册02—程序接口

3 - 程序接口(API) 这个部分描述了 Lua 的 C API , 也就是宿主程序跟 Lua 通讯用的一组 C 函数. 所有的 API 函数按相关的类型以及常量都声明在头文件 lua.h 中. 虽然我们说的是“函数”,但一部分简单的 API 是以宏的形式提供的. 所有的这些宏都只使用它们的参数一次 (除了第一个参数,也就是 lua 状态机), 因此你不需担心这些宏的展开会引起一些副作用. 在所有的 C 库中,Lua API 函数都不去检查参数的有效性和坚固性. 然而,你可以在编译 Lua 时

bash参考手册之五(shell变量)续三

LINENO 当前在执行的脚本或者shell函数的行号. LINES 命令select用来确定打印选择列表的列宽.收到SIGWINCH后,自动设置. MACHTYPE 是一个字符串,描述了正在运行Bash的系统的类型,描述的格式符合GNU cpu-company-system 标准. MAILCHECK 确定多长时间间隔(以秒为单位),shell要去由变量MAILPATH和MAIL的值指定的文件中,检查邮件.默认值是60秒.当检查邮件的时间到了,shell在显示提示符前执行检查动作.如果这个变量

参考手册目录__第三部分

参考手册目录__第一部分 参考手册目录__第二部分 参考手册目录__第三部分 第 26 章: 扩展与嵌入Python 26.1 扩展模块------482  26.1.1 扩展模块原型------484 26.1.2 命名扩展模块------486 26.1.3 编译与打包扩展------486 26.1.4 从Python 到 C 语言的类型转换------488 26.1.5 从C 到 Python 的类型转换------492 26.1.6 给模块添加值------493 26.1.7 错

KnockoutJS 3.X API 第三章 计算监控属性(5) 参考手册

计算监控属性构造参考 计算监控属性可使用以下形式进行构造: ko.computed( evaluator [, targetObject, options] ) - 这种形式是创建一个计算监控属性最常见的情况. evaluator - 用于返回计算值的函数. targetObject-如果给出定义的值this时KO调用回调函数.参见部分第三章 计算监控属性(1) 使用计算监控属性以获取更多信息. options - 计算监控属性的其他属性的对象.请参见下面的完整列表. ko.computed(

Lua1.1 Lua 的设计和实现 (一)

转载出处:http://my.oschina.net/xhan/blog/309613 说明: 这个文档是 Lua1.1 的 doc 目录里的 lua.ps 文件. 同时这个文档可以这里找到:http://www.lua.org/semish94.html 原文版权归原作者所有,这篇翻译只是作为学习之用.如果翻译有不当之处,请参考原文. --------------------以下是正文------------------ 应用程序扩展语言的设计和实现 摘要.我们描述 Lua 的设计和实现,一个

EL表达式参考手册

一.EL简介 1.语法结构     ${expression}2.[]与.运算符     EL 提供.和[]两种运算符来存取数据.    当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符号,就一定要使用 [].例如:        ${user.My-Name}应当改为${user["My-Name"] }    如果要动态取值时,就可以用[]来做,而.无法做到动态取值.例如:        ${sessionScope.user[data]}中data 是一个变量3