2.lua的类型和值
2015-05-27 17:56
246 查看
--------------------------2.1-类型和值---------------------------------------------
--8个基本类型:nil boolean number string userdata function thread table
--[[
print(type("Hello World")) -->string
print(type(10.4*3)) -->number
print(type(print)) -->function
print(type(type)) -->function
print(type(true)) -->boolean
print(type(nil)) -->nil
print(type(type(x))) -->string
----------------------------------------------------
print(type(a)) -->nil
a=10
print(type(a)) -->number
a="a string"
print(type(a)) -->string
a=print
print(type(a)) -->function
--]]
----------------------------------------------------
--[[
a = "one string";
b = string.gsub(a,"one","another")
print(a) -->one string
print(b) -->another string
--]]
----------------------------------------------------
--转义符--
--[[
\a bell
\b back space
--后退
\f form feed
--换页
\n newline --换行
\r carriage return
--回车
\t horizontal tab
--制表
\v vertical tab
\\ backslash
--"\"
\" double quote
--双引号
\' single quote
--单引号
\[ left square bracket --左中括号
\] right square bracket --右中括号
--]]
-----------------------------------------------------
--print("one line\nnext line\n\"inquotes\",'in quotes'")
--[[
运行结果:
one line
next line
"inquotes",'in quotes'
--]]
--print('a backslash inside quotes:\'\\\'')
-->a backslash inside quotes:'\'
--print("a simpler way: '\\'") -->a simpler way: '\'
------------------------------------------------------------
--使用[[...]]表示字符串--
page=[[
<HTML>
<HEAD>
<TITLE>An HTML Page</TITLE>
</HEAD>
<BODY>
Lua
a text between double brackets
</BODY>
</HTML>
]]
io.write(page)
--------------------------------------------------------
--[[
print("10"+1) -->11
print("10+1") -->10+1
print("-5.3e-10"*"2") -->-1.06e-009
--print("hello"+1) -->ERROR(cannot convert "hello")
print(10 .. 20) --1020
--]]
---------------------------------------------------------
--[[
line = io.read()
n=tonumber(line)
if n==nil then
error(line .. " is not a valid number")
else
print(n*2)
end
--]]
print(tostring(10)=="10") -->true
print(10 .. "" == "10") -->true
--8个基本类型:nil boolean number string userdata function thread table
--[[
print(type("Hello World")) -->string
print(type(10.4*3)) -->number
print(type(print)) -->function
print(type(type)) -->function
print(type(true)) -->boolean
print(type(nil)) -->nil
print(type(type(x))) -->string
----------------------------------------------------
print(type(a)) -->nil
a=10
print(type(a)) -->number
a="a string"
print(type(a)) -->string
a=print
print(type(a)) -->function
--]]
----------------------------------------------------
--[[
a = "one string";
b = string.gsub(a,"one","another")
print(a) -->one string
print(b) -->another string
--]]
----------------------------------------------------
--转义符--
--[[
\a bell
\b back space
--后退
\f form feed
--换页
\n newline --换行
\r carriage return
--回车
\t horizontal tab
--制表
\v vertical tab
\\ backslash
--"\"
\" double quote
--双引号
\' single quote
--单引号
\[ left square bracket --左中括号
\] right square bracket --右中括号
--]]
-----------------------------------------------------
--print("one line\nnext line\n\"inquotes\",'in quotes'")
--[[
运行结果:
one line
next line
"inquotes",'in quotes'
--]]
--print('a backslash inside quotes:\'\\\'')
-->a backslash inside quotes:'\'
--print("a simpler way: '\\'") -->a simpler way: '\'
------------------------------------------------------------
--使用[[...]]表示字符串--
page=[[
<HTML>
<HEAD>
<TITLE>An HTML Page</TITLE>
</HEAD>
<BODY>
Lua
a text between double brackets
</BODY>
</HTML>
]]
io.write(page)
--------------------------------------------------------
--[[
print("10"+1) -->11
print("10+1") -->10+1
print("-5.3e-10"*"2") -->-1.06e-009
--print("hello"+1) -->ERROR(cannot convert "hello")
print(10 .. 20) --1020
--]]
---------------------------------------------------------
--[[
line = io.read()
n=tonumber(line)
if n==nil then
error(line .. " is not a valid number")
else
print(n*2)
end
--]]
print(tostring(10)=="10") -->true
print(10 .. "" == "10") -->true
相关文章推荐
- Lua程序设计(第2版)第二章学习随笔——类型和值
- 【lua学习笔记】第二章:类型和值
- Lua 类型和值
- Lua - 类型和值
- Lua中文教程学习笔记 (1) 类型和值
- lua中文教程(第二章 类型和值)
- lua学习笔记之类型和值
- Lua基础 类型和值(一)
- (LUA教程)第2章 类型和值
- Lua程序设计:第2章 类型和值
- lua 基础 2 类型和值
- Lua基础(一):类型和值
- Lua基础 类型和值(二)
- lua--lua中的类型和值
- Lua_ 第 2章 类型和值
- Lua学习笔记(一):类型和值
- c/c++_Lua交互----关于Lua中table类型的使用实例
- C#中引用类型和值类型
- OOP03~05-Java变量和值的数据类型及其运算
- Lua教程(三):值与类型介绍