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

转载出处:http://my.oschina.net/xhan/blog/310019

(接上篇)

--------------------------------------

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(' ' .. 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,为这个语言起了个名字。

---------

原文地址:https://www.cnblogs.com/vd01/p/4939514.html