Lua is a dynamic type language. You do not need to declare variables or specified types. The first assignment is declaration. speaking of this, I think this feature of Dynamic Language is quite similar to Assembly: No matter what type you are, it represents a memory address for the underlying layer, the operator represents the operations on these addresses and the quantity in these addresses. let's talk about Pil. Today's content is: Types and values.
Note content:
1. lua provides eight data types: nil, Boolean, String, number, userdata, function, thread, table. if you want to see the type of an expression, you can use the type () function. Its return value is of the string type:
& Gt; print (type (10.4*3 ))
Number
> Print (type (10.4*3 )))
String
2. A variable can be assigned multiple times with any type of value. in contrast, the usage of function pointers in Lua is quite simple: you no longer need to get a pointer like in C, for example: a = print, then we can use a (3) to print the expression value. what makes me feel better is that using table + function assignment can easily implement function tables, so as to avoid a large number of if else statements. format:
Function func_1 (para)
Para = para + 1
Return para
End
Function func_2 (para)
Para = para + 2
Return para
End
Func_table = {
["Func1"] = func_1,
["Func2"] = func_2,
}
Print (func_table ["func1"] (1 ))
The printed result is:
2
Select string as the key to help you understand how convenient the function table is in Lua.
3. Before the first value assignment, the global variable defaults to nil. Deleting the global variable is also used to assign the value to nil. nil, which is also often used as the invalid return value of the function.
4. boolean has two values: false and true (all in lower case ). in a condition expression, only false and nil indicate false, and any other value indicates true (0 also indicates true ). for example, these values:
If false then print ("OK") End
If nil then print ("OK") End
If "" Then print ("OK") End
If ''then print ("OK") End
If "Haha" then print ("OK") End
If 0 then print ("OK") End
In the preceding statements, only the first two statements cannot print OK.
5. for the number type, Lua only provides the real number. there is no integer type, according to Pil: as it does not need it .... moreover, most modern CPUs do floating-point arithmetic as fast as (or even faster than) integer arithmetic. I think Lua's own "sophistry" is already convincing enough, and it's no longer necessary. what is freedom? Here:
> Print (string. sub ("hello", 1.1, 2.2 ))
He
The effect is equivalent to print (string. sub ("hello", 1, 2 ))
6. it is said that the string type function of Lua is very powerful, but for my current use, I do not feel the more convenience this powerful brings to me, I think, the Lua code may be missing. the string type in Lua can store any long string, a string that cannot be changed once created. some people say, isn't this very inefficient? Actually, it's not all you need. The functions that operate on strings: Sub, find, copy, replace, Cat, etc. many of them do not require modifying the strings themselves. Many functions do not modify strings. At the same time, they modify the operations of strings,
It is better to create a new string directly. lua's memory management on the string is great. Programmers don't have to worry about its allocation and recovery. Lua will decide when to release the string based on its own reference counting mechanism, at the same time, it is precisely because the string itself cannot be modified that the Lua string operation has become a simple reference operation, which should be more efficient.
String constant, which can be enclosed in single or double quotation marks. however, we need to cultivate a good programming habit: using a style of declarative form. escape characters, and add "/" to the front "/". the number that follows a slash (/) indicates an ASC character, but the number of a slash (/) cannot exceed three digits.
When defining a string containing multiple rows, you can use the [[] identifier, so that you do not need to add/N to each row for escape.
Lua provides automatic conversion between string and number types. the so-called automatic conversion determines whether the current operation is processed by string or numeric Based on the function behavior during the call, for example, "10" + 1, in this case, it will be processed by number, because the character string is not added. to connect two strings, use .. operator. and "10 ".. 1. The result is 101. of course, operations like "A" + 1 are invalid because the Lua conversion is not based on ASI, but is equivalent to executing a atoi ("10 "), then add 1.
In other cases, if you want to convert a string to a number, use tonumber. If the conversion fails, return nil. for example, the return value of tonumber ("A") is nil. to convert a number to a string, you can use either the tostring function or the connector to connect the number to an empty string: 10 .. "", the result is: "10"