Lua scripting language

Source: Internet
Author: User
In this article, I would like to introduce you to Lua program design. I suppose everyone has learned at least one programming language, such as basic or C, especially C. Because Lua is used as a script in the Host Program for the maximum purpose. Lua's syntax is relatively simple and easier to learn, but its functions are not weak. In Lua, everything is a variable except a keyword. Remember this sentence. I. The first step is to write a program with annotations. In Lua, you can use single-line and multi-line annotations. In a single line comment, two consecutive minus signs "--" indicate the start of the comment until the end of the line. It is equivalent to "//" in C ++ "//". In multi-line comments, the Comment starts from "-- [" and continues. This annotation is equivalent "/*... */". In comments, "[[" and "]" can be nested. Ii. Lua programming classic "Hello World" programs are always used to start introducing a language. In Lua, writing such a program is very simple: Print ("Hello World") in Lua, the statements can be separated by semicolons ";", or blank. Generally, if multiple statements are written in the same row, we recommend that you use semicolons to separate them. Lua has several program control statements, such as conditional control: If condition then... Elseif condition then... Else... End while loop: While condition do... End repeat loop: Repeat... Until condition for loop: for variable = initial value, end value, step do... End for loop: for variable 1, variable 2 ,..., Variable N in table or enumeration function do... End note: The for loop variable always acts only on the partial variable of the for, you can also omit the step value, at this time, the For Loop will use 1 as the step value. You can use break to stop a loop. If you have the foundation of programming, such as basic and C, you will feel that Lua is not difficult. However, Lua has several differences from these programming languages, so pay special attention to them.. The block statement block is enclosed by "{" and "}" in C ++. In Lua, It is enclosed by do and end. For example: Do print ("hello") end you can set local variables in the function and statement block. The value assignment statement is enhanced in Lua. It can assign values to multiple variables at the same time. For example, a, B, c, d = 1, 2, 3, 4, or even a, B = B, a -- how convenient the variable switching function is. By default, variables are always considered global. If you want to define a local variable, you must use local to describe it during the first assignment. For example, local a, B, c = 1, 2, 3 -- A, B, and C are all local variables. The numerical computation is the same as that in C. It supports the following: + ,-,*,/. But Lua has another "^ ". This indicates exponential multiplication. For example, the result of 2 ^ 3 is 8, and the result of 2 ^ 4 is 16. Connect two strings. You can use the ".." operator. For example, "this a"... "string." -- equal to "this a string". Comparison operation <><=== ~ = Represents less than, greater than, not greater than, not less than, equal, not equal. All these operators always return true or false. For Table, function, and userdata data, only = and ~ = Available. The two variables reference the same data. For example, a = {1, 2} B = a print (A = B, ~ = B) -- true, false A = {1, 2} B = {1, 2} print (A = B, ~ = B) -- false, true. logical operation and, or, not, where, and or differ greatly from C. Here, remember that in Lua, only false and nil are calculated as false, and any other data is calculated as true, and 0 is also true! The operation result of and or is not true or false, but related to its two operands. A and B: If a is false, A is returned; otherwise, B a or B is returned. If a is true, A is returned; otherwise, B is returned. For example: print (4 and 5) --> 5 print (nil and 13) --> nil print (false and 13) --> false print (4 or 5) --> 4 print (false or 5) --> 5 this is a very useful feature in Lua, and it is also a hybrid feature. We can simulate the C language statement: x =? B: C. In Lua, it can be written as: x = A and B or C. The most useful statement is: x = X or V, which is equivalent to: If not X then x = V end.. Operator priority. The order from high to low is as follows: ^ not-(mona1 operation) */+-... (string connection) <><== ~ = And oriii. Keywords cannot be used as variables. There are not many Lua keywords, and break do else elseif end false for function if in local nil not or repeat return then true until whileiv. how can I determine the type of a variable? You can use the type () function to check. Lua supports the following types: Nil null, all unused variables are nil. Nil is both a value and a type. Boolean value: number value. In Lua, the value is equivalent to a double string in C language. If you want to, the string is a table relational table type that can contain ''characters, this type of function is quite powerful. Let's talk about it later. Function type. Do not doubt that a function is also a type. That is to say, all functions are a variable. Userdata: Well, this type is used to deal with Lua's host. Generally, the host is written in C and C ++. In this case, userdata can be any data type of the host, usually including struct and pointer. Thread thread type. There is no real thread in Lua. In Lua, a function can be divided into several parts for running. If you are interested, you can go to Lua's documents. V. variables are used in all languages. In Lua, no matter where you use variables, you do not need to declare them, and all these variables are always global variables unless you add "local" in front ". Pay special attention to this, because you may want to use local variables in the function, but forget to use local to describe. Variable names are case-sensitive. That is to say, a and a are two different variables. To define a variable, assign a value. "=" Operations are used to assign values. Let's define several common types of variables together. A. nil as mentioned earlier, the values of unused variables are all nil. Sometimes we also need to clear a variable. At this time, we can directly assign the variable a nil value. For example, var1 = Nil -- note that nil must be in lowercase. boolean values are usually used for condition determination. There are two types of Boolean values: true and false. In Lua, only false and nil are calculated as false, and all other types of values are true. For example, 0 and null strings are all true. Do not be misled by the C language habits. 0 is true in Lua. You can also assign a value of the boolean type to a variable. For example, varboolean = true c. Number in Lua does not have the integer type. Generally, as long as the value is not very large (for example, it cannot exceed 100,000,000,000,000), there will be no rounding error. In many CPUs, real number operations are not slower than integers. The representation of real numbers, similar to the C language, such as: 4 0.4 4.57e-3 0.3e12 5E + 20 d. String string, is always a very common advanced type. In Lua, you can easily define long and long strings. There are several methods to represent a string in Lua. The most common method is to enclose a string with double quotation marks or single quotation marks, such as: "This is a string. "similar to the C language, it supports some escape characters, the list is as follows: 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 because this string can only be written in in a row, therefore, it is inevitable to use escape characters. The strings added with escape characters seem to be not flattering, for example: "One linennext linen" in quotes ", 'in quotes'" a lot of "" symbols make people look quite appetizing. If you share the same feelings with me, we can use another Representation Method in Lua: Enclose strings of multiple rows with "[[" and, for example: page = [[<HTML> 

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: 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.