Learning Lua programming (2) Basic Lua syntax example

Source: Internet
Author: User
Tags print print

Start to learn Lua programming. First, start with some simple syntax.

I. Editing Environment

We recommend a Lua programming IDE, which is very powerful. Zerobrane studio is available on Windows and Mac platforms. Click Open link official

2. Start some simple programming (see the example from http://blog.csdn.net/xiaominghimi/article/details/8770395)

For detailed syntax, refer to my previous post (reposted) blog


-- Single-line comment statement -- [[comment paragraph statement] -- references other Lua files and does not need to be added (. lua) Suffix -- require "XX" -- variables do not need to be defined, you can directly assign COUNT = 100 -- member variable local COUNT = 100 -- local variable -- method definition function Hello (...) -- print Print ("Hello Lua! "); Print (string. format (...)) end -- no separator is required for each line of code. Of course, you can also add -- access the uninitialized variable. Lua returns nil by default -- call function form Hello ("you know") -- output: -- Hello Lua! -- You know -- print the variable type isok = falseprint (type (isok) -- output: -- Boolean -- the value of the basic variable type A = Nil -- Lua is nil, which is equivalent to deleting B = 10C = 10.4d = false -- defines the string, single quotes, double quotation marks can be e = "I am" d = 'himi' -- the two strings can be connected in the following form: stringa = "hi" stringb = "mi" -- can be used ".. "symbol, connecting two strings print (stringa .. stringb) -- output: -- himi -- In addition, Lua also supports transfer characters, as shown in the following print (stringa .. "\ n ".. stringb); -- output: -- hi -- mi -- modify part of the gsub of the string in the following format: (modify the HI in the stringa string to wt) stringa = string. gsub (stringa, "Hi", "WT") print (string A); -- output: -- wt -- replace the character with the number tonumber (it will be converted automatically if it is not converted) -- replace the number with the character tostring (it will be converted automatically if it is not converted) stringc = "100" stringc = tonumber (stringc) stringc = stringc + 20 stringc = tostring (stringc) print (stringc) -- output: -- 120 -- use # print (# stringc) to get the length of a string -- output: -- 3 -- Create Table tablea = {} M = "X" tablea [m] = 100m2 = 'y' tablea [m2] = 200 print (tablea ["X"] .. "\ n ".. tablea. y) -- output: -- 100--200 -- In addition, the table can also be in the following format (starting from 1) tableb = {"4", "5", "6", "7 ", "8"} print (tableb [1]) -- output: -- 4 -- Arithmetic Operator C1 = 10 + 2C2 = 10-2c3 = 10*2c4 = 10/2 C5 = 10 ^ 2c6 = 10% 2c7 =-10 + 2 print (C1 .. "_".. c2 .. "_".. c3 .. "_".. c4 .. "_".. c5 .. "_".. c6 .. "_".. c7) -- output: -- 12_8_20_5_100_0_-8 -- control operation -- if then elseif then else endabc = 10if abc = 10 then print ("V1 ") elseif abc = 9 then print ("V2") else print ("V3") end -- output: -- V1 -- for variable = initial value, end value, step do... end -- increases from 4 (the first parameter) to 10 (the second parameter). The Unit of each increase is 2 (the third parameter). For I =, 2 do print ("for1: ".. I + 1) end --[ [Output: for1: 5for1: 7for1: 9for1: 11] -- you can leave the last parameter Unspecified. The default growth rate of 1 is for I = 4, 10 do print ("for2: ".. I + 1) end -- [Output: for2: 5for2: 6for2: 7for2: 8for2: 9for2: 10for2: 11] tablefor = {"himi1", "himi2 ", "himi3", "himi4", "himi5"} for K, V in pairs (tablefor) Do print ("for3: Key :".. k .. "value :".. v) end -- [Output: for3: Key: 1 Value: himi1for3: Key: 2 value: himi2for3: Key: 3 value: himi3for3: Key: 4 value: himi4for3: key: 5 value: himi5] -- whilew1 = 20 while tr UE do W1 = W1 + 1 If W1 = 25 then break endendprint ("while :".. w1) -- output: -- while: 25 -- repeat AA = 20 repeat AA = AA + 2 print ("Repeat :".. aa) until AA> 28 -- [Output: Repeat: 22 repeat: 24 repeat: 26 repeat: 28 repeat: 30] -- Relational operators -- do not equal to the symbol ~ = Instead! = AX = 10Bx = 20 if Ax> BX then print ("GX1") elseif ax <BX then print ("GX2") elseif ax> = Bx then print ("GX3 ") elseif ax <= Bx then print ("gx4") elseif AX = Bx then print ("gx5") elseif ax ~ = Bx then print ("gx6") else print ("gx7") end -- output: -- GX2

-- Function description -- function funtestbackone (acount) with a returned value: Local acount = acount + 1 return acountend A = 20 print (funtestbackone ()) -- function funtestbackmore () return 2, 3end a, B = funtestbackmore () print (.. "and ".. b) -- function funtestunknow (...) with Variable Length Parameters (...) print (...) endfuntestunknow (a, B, "himi") -- closed function (one function is written in another function) function funtest1 (...) local d = 12; D = d +... function funtest2 (...) print (d) End funtest2 () end funtest1 (100) -- [[Print: 212 and 323himi112]

These are simple foundations. Let's get started with the syntax.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.