一段lua程序,求指导为什么输出是这个
就我个人的理解来看,使用LUA作为脚本语言有三个好处:1、轻量级LUA只包括一个精简的核心和最基本的库。这使得LUA体积小、启动速度快,从而适合嵌入在别的程序里。2、可配置性、可扩展性LUA并不象其它许多”大而全”的语言那样,包括很多功能,比如网络通讯、图形界面等。但是LUA可以很容易地被扩展:由宿主语言(通常是C或C++)提供这些功能,LUA可以使用它们,就像是本来就内置的功能一样。3、兼容性LUA由标准C编写而成,几乎在所有操作系统和平台上都可以编译,运行。这些都是LUA本身所具备的一些特质。而这些特质正好就决定了LUA的广泛使用:1、轻量级没人希望自己的应用程序需要附加几十M甚至几百M的库类文件才能运行。如果一个应用程序这么做了,那么它很难做到RunAnywhere。当然,这个RunAnywhere只是这么一说。2、可配置性、可扩展性在游戏研发的过程中,修改是必不可少的过程。如果每次修改都需要从底层做起,那必定是一个非常繁琐的过程。而LUA的可配置、扩展正好可以解决这一问题。3、兼容性游戏只是游戏,不能决定玩家的机器配置,更不能决定玩家的操作系统。如果因为游戏与用户的操作系统不兼容导致游戏销量下降,我想,这肯定是不被允许的。所以,具备了以上三个特点的LUA,没有不作为游戏脚本使用的理由。
LUA怎么循环输出一个表里的所有元素
local t = {444,555,”fff”} for i,v in pairs(t) do print(i,v) end
用C语言或者LUA将word里的文件输出
则1word为2byte;32位时,1word为4byte:bit这些都是存贮单位:一个二进制位;byte:包含8bit;word:系统硬件有关,数据总线为16位,word一般叫作“字”
lua 读取文本,并输出在文本中
应该这样写 local f = io.open(“test.txt”,”a”) local t = f:write(“1111111″,”\n”) f:close()
用lua实现一个效果,输入一个字符串,输出该字符串中所有的字符组合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env lua
function th_table_dup(ori_tab) — 复制table
if (type(ori_tab) ~= “table”) then
return nil;
end
local new_tab = {};
for i,v in pairs(ori_tab) do
local vtyp = type(v);
if (vtyp == “table”) then
new_tab[i] = th_table_dup(v);
elseif (vtyp == “thread”) then
— TODO: dup or just point to?
new_tab[i] = v;
elseif (vtyp == “userdata”) then
— TODO: dup or just point to?
new_tab[i] = v;
else
new_tab[i] = v;
end
end
return new_tab;
end
function permutation(s1,p)
if #s1 == 1 then — 如果s1长度为1,意味着获得了一种新的排列
p = p .. s1[1] — 那么就打印它
print(p)
else — 否则,选定一种方案,继续递归
local i
for i = 1, #s1 do — 4.3 Control Structure; Numeric for
local p2, s2
p2 = p .. s1[i]
s2 = th_table_dup(s1) — 把s1表的内容复制给s2表
table.remove(s2,i) — 20.1 Insert and Remove
permutation(s2,p2) — 用s2继续进行递归
end
end
end
— 主程序
a = io.read() — 读入字符串,可含汉字
— 22.1 The Simple I/O Model
len = #(string.gsub(a, “[\128-\191]”, “”)) — 计算字符数(不是字节数)
i=1 — 迭代出每一个字符,并保存在table中
s = {} — 21.1 Basic String Functions
for c in string.gmatch(a, “.[\128-\191]*”) do — 21.2 Pattern-Matching Functions
lua语言读取输出TXT文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
你可参考如下代码:
local lines={}
local i=0
local f=io.open(filename,’r’)
local s=”
repeat
s=f:read(‘*l’)
if s~=nil then
i=i+1
lines[i]=s
end
until s==nil
io.close(f)
local num=i
LUA循环输入字符串
不知道那个XXX是不是随机的
我先写了个随机生成XXX
如果你要固定 就把变量rd 改成你要的字符串
生成001-100只需要用格式化字符串就行了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
local function main ()
local stringId = ‘”459373440″‘
math.randomseed(os.time())
for i= 1, 100 do
local rd = math.random( 0, 999)
stringId = string.format( ‘”%03d%03d”‘, rd, i )
print(stringId )
end
–os.execute(“input text “..stringId)
end
main ()
linux如何格式化输出内容
可以使用printf ,如:# printf ‘%15s\t\n’ hello word hello word…
lua语言的 format怎么将时间3:01转换成03:01啊,谢谢
str = "3:01" _, _ , hour, minute = string.find(str, "(%d+):(%d+)") print(string.format("%02d:%02d",hour, minute))
lua metamethod有多少方法可重写
写过Java或者C#的人都知道,Object类中都有一个tostring的方法,程序员可以重写该方法,以实现自己的需求.在Lua中,也是这样的,当我们直接print(a)(a是一个table)时,是不可以的.那怎么办,这个时候,我们就需要自己重新定义__…