(i) LUA basic syntax

Source: Internet
Author: User

1. Starting from Hellowrold
1 -- syntax is similar to Python, and you can use print directly like Python 2 -- Here I can write the Chinese directly, which is obviously regarded as a comment. In Lua, two-represents a comment 3--[[4     This form can represent a multiline comment 5]]6  7-- in Lua, a semicolon can be appended to a statement, but it can also be added without.  8print("helloWorld")

basic types of 2.lua
1 --Lua, like Python, does not need to declare a variable like C because it has a type of value2 --assign values directly to variables3 4 --[[5 the basic types of LUA are: nil (empty)6 Boolean (Boolean)7 number (numeric)8 String (String)9 Table (Tables)Ten function, and so on . One  A ]] -  - --[[ the Note: When assigning a value, you can write multiple statements in a single line, separated by a space or;  - This is just a demo, but it's not recommended in real-world development . - ]] -A =123; b="Satori"; c=3.14;d =Math.PI + --to view the type of a value using type - Print(type(a),type(b),type(c),type(d))--Number string number number + --As you can see, there is no int float in Lua and so on, as long as it is a number, type all A Print(type(Print))--function at  - --If a variable we are not assigned to use directly, in Python will be an error, prompting the name XXX is not defined - --However, there is no error in Lua, and for an unassigned variable, the default is nil in Lua . - --Nil is used to differentiate other variables, assigning a global variable to nil equals deleting the global variable - Print(MMP,type(MMP))--Nil Nil -  in --in Lua, only false and nil represent false, and others are true. 0 and empty strings are true in Lua, which is not the same as Python -  to --in Lua, once a string has been created, it cannot be modified. This is the same as Python, but you can use replace to modify and return a new string in Python + --then Lua has a similar approach, and the method also gets a return value that does not modify the original value -S1 ="Hello cruel world" theS2 =string.gsub(S1,"Cruel","beautiful") * Print(S1)--Hello cruel world $ Print(S2)--Hello beautiful WorldPanax Notoginseng  - --and in Lua, you can use single quotes or double quotes the --similar to Python, if the input string itself has single quotes, then the outer is double-quoted, and vice versa + Print("I ' m Satori")--I ' m Satori A --if I have to use the same quotation marks, you can use the escape character to escape the inner quotation marks . the Print('i\ ' m Satori')--I ' m Satori +  - --[[ $ some escape characters $ \a: Bell - \b: Backspace - \f: Provide form the \ n: Line break - \ r: EnterWuyi \ t: watchmaking]] the Print("I am \ r Satori, come from \ n Oriental Spiritual Temple") - --[[ Wu Satori, come from - Oriental Spiritual Temple]] About  $ --The string can also be represented using \ plus a three-digit number, which is the corresponding ASCII table - Print("\065")--A -  - --If you want to express multiple lines of string, you can use [[]], similar to the three quotes in Python "" AS3 =[[ + The village Lori cheated me old and powerless the often happen with me sex - Relationship $ ]] the Print(S3) the --[[ the The village Lori cheated me old and powerless the often happen with me sex - Relationship in ]] the  the --if a string and a number are calculated, then the string is tried to be converted into a number. About --obviously the string must be made up of numbers, and if it contains a character that is not a number, it will be an error and cannot be converted the Print("666"+1)--667 the  the --string connection, in Lua: Represents a string connection + --It is not possible to use the form "a" + "B", which is allowed in Python, but not in Lua. But you can use: -S4 ="Satori" the Print("a".."b".. S4)--AB SatoriBayi Print("1".."+".."1".." == ".."2")--+ = 2 the  the --What if I want to get the length of a string? Use the Len function in Python, but use # in Lua.  - --and # in Python notation, so the two languages are similar in some places, some are completely different, it's really interesting - Print(#"I casually write the standard 15 words")-- $ the --The output is 45, eh, not 15? I must have found the answer at one glance, a kanji account of three bytes the --and Lua is using UTF-8 encoding by default. It is worth mentioning that the results of using the Len function in Python are calculated as the --because in Python, Len calculates the number of characters, not the number of bytes.  the --and in the go language, the result of the calculation is also 45, which is also a byte. If you want the number of characters, you need to convert []rune] in go.  -S6 ="MMP" the Print(#s6)--3 the  the --There is also a type called table in Lua, which is a dictionary (hash table) in Python, and key and value one by one correspond94Hash_map = {} thehash_map["Satori"] ="Oriental Spiritual Temple" the Print(hash_map["Satori"])--Oriental Spiritual Temple the --assigns a hash_map to another variable and deletes the Hash_map98Hash_map_1 =Hash_map AboutHash_map =Nil - Print(HASH_MAP)--Nil101 Print(hash_map_1["Satori"])--Oriental Spiritual Temple102 --you can see that even if hash_map is removed, it does not affect HASH_MAP1103 --Lua garbage collection is similar to the reference count in Python, and when HASH_MAP1 is also assigned nil, the garbage resource is deleted when no variable is referenced.104 --If you access a non-existent key, you get nil . thex = {}106x[1] =2107x[2] =3108 Print(x[1], x[2], x[3])--2 3 Nil109 --to remove an element from a table, assign a value of nil directly thex[1] =Nil111 Print(x[1])--Nil the --and table This structure, you can also use. To access113X.name ="Satori" the Print(X.name)--Satori

(i) LUA basic 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.