Lua as a good embedded language can be very good as a C + + supplement, in the gaming industry is getting a wide range of applications
have been thinking, can you bring him into the business area of the rules of language? Refining the parts of a business rule that are often changed,
Without recompiling the program. Use as a role for the rule engine
Before use, of course, you have to install a http://www.lua.org/download a Lua_v5.1.4.23.exe installation, after loading
You can use Lua.exe to explain it line by row, or write a script to execute with Lexecutor.wlua
1) First, a HelloWorld.
Copy Code code as follows:
> print ' HelloWorld '
HelloWorld
> Print ("HelloWorld")--that's what the note says!
HelloWorld
> a= ' Hello '
> Print (a)
Hello
2) Main types
Copy Code code as follows:
> a=1
> b= "ABC"
> c={}
> D=print
>
> Print (Type (a))
Number
> Print (type (b))
String
> Print (Type (c))
Table
> Print (Type (d))
function
>
As shown above, there are four types of Lua, that is, numbers, strings, table (which actually understands the object that can be interpreted as JavaScript), and function types,
Of course, there are the most common type of bool, True and false, (not considering nil, and userdata,thread, etc.)
A variable of a function type, plus "()" that can be executed, as
Copy Code code as follows:
3 variables and constants, strings
Copy Code code as follows:
> a,b,c,d = 1,2, ' C ', {1}
> Print (a,b,c,d)
2 c table:0041bc58
> a= ' single ' quoted ' string and Double \ quoted\ ' string inside '
> b= ' single \ ' quoted\ ' string and double quoted ' string inside '
> c= [[Multiple line
>> with ' single '
>> and "double" quoted strings inside.]]
> Print (a)
Single ' quoted ' string and double ' quoted ' string inside
> Print (b)
Single ' quoted ' string and double ' quoted ' string inside
> Print (c)
multiple Line
With ' single '
and double quoted strings inside.
> A=a ... See I'm connected to " --string with two ... You can connect
> Print (a)
single ' quoted ' string and double ' quoted ' string inside see I connect
Let's focus on what's special about Lua.
1. When declaring variables and assigning values to variables, you can assign values together with multiple variables
2. String constants can be used in double quotes and single quotes and are mixed without escaping characters, with PHP type, and by "[[]]"
This is similar to the string in C # that can be added with the @ symbol to preserve the formatting within the string
4) Logical Control statement
Copy Code code as follows:
> a=10-Logic control statement It's no bother, anyway, it's a bit like pl/sql.
> If a==10 Then
>> print ("I am 10")
>> ElseIf a== Then
>> print ("I am 11")
>> Else
>> print ("I am equal in the water ocean")
>> End
I'm a 10.
5) Circulation structure
Copy Code code as follows:
>--the first while loop
> a=1
> While a~=10 do
>> Io.write (A... "")
>> a=a+1
>> End
1 2 3 4 5 6 7 8 9 >
>--The second kind of repeat, until cycle
> a=0
> Repeat
>> a=a+1
>> Print (a)
>> until a==5
1
2
3
4
5
>--third for loop
> For a=1,4 do
>> Io.write (a)
>> End
1234>
>-More specifically, there is a step-by-step concept, the following is a step by 2
13> for a=1,4,2 do
>> Io.write (A.. ' ')
>> End
1 3 > for a=1,5,2 do
>> Io.write (A.. ' ')
>> End
1 3 5 >
6) function and its use
Copy Code code as follows:
>--1. First, the simplest.
> Function myFunc1 ()
>> print ("Hello function")
>> End
> myFunc1 ()
Hello function
>--2. Another one with a parameter.
> a=1
> b=2
> Function MyFunc2 (PAR1,PAR2)
>> return PAR1+PAR2
>> End
> C=myfunc2 (a,b)
> Print (c)
>--3. Can even return multiple variables
> a=1
> b=2
> Function myFunc3 (a,b)
>> return B,a
>> End
> c = myFunc3 (a,b)--c will be equal to the first returned, which in turn is 2.
> Print (c)
> c,d=myfunc3 (A,B)--Of course, if you assign values to multiple variables, there's no problem.
> Print (C,D)
1
> Print (myFunc3 (a,b))--Print all out directly
1
>--4. Can also be a variable parameter
> Function MyFunc4 ()
>> print ()
>> End
> MyFunc4 (a,b,c,d)
2 2 1
7 table (I think understanding object can also be used)
Copy Code code as follows:
> linbc={}
> linbc.sex = "Male"
> linbc.age = 27
> Print (linbc,linbc.sex,linbc["age"])
TABLE:0041ED50 Male 27
> A=LINBC
> Print (a["Sex"])
Man
> Print (a)--reference type, see A and LINBC memory address is the same
Table:0041ed50
>-Look at this usage, which is basically equivalent to JavaScript usage.
>-can actually be understood as a hashmap-like key value pair
> For i,v in Pairs (LINBC) do
>> Print (I,V)
>> End
Age 27
Sex male
>-can also be used as an array, subscript is starting from 1 OH
> arr={1,2,3, "A", ' B ', LINBC}
> For i,v in Pairs (arr) do
>> Print (I,V)
>> End
1
2
3
A
B
Table:0041ed50
>-Since it's an array, it's definitely going to add to the record.
> Table.insert (arr,a)
> Table.foreach (arr,function (i,v) print (v) end)
A
B
Table:0041ed50
Table:0041ed50
>-see above, you can foreach, you can use anonymous function oh
>-Of course, you can also use the subscript to access the
> Print (arr[1])
>-Call Table.insert will automatically add subscript, such as
> Table.insert (LINBC, "Hello")
> Print (arr[1])
1