Concise summary of operators in Lua _lua

Source: Internet
Author: User
Tags lua

1. Digital operation

Support +,-, *,/,^. The operands of these operators are all real numbers.

2. Connection operation

Connect two strings, using the "..." Operator.
If the operand is a number, LUA converts the number to a string.

3. Assignment operation

To assign a value with a multivariate:

Copy Code code as follows:

a,b,c,d = ' www.jb51.net ', ' Q Group: 14624678′,1,2

Variable Exchange:
Copy Code code as follows:

A,b=b,a

By default, variables are always considered global. If you need to define a local variable, you need to use the locals description when assigning the first time. Such as:
Copy Code code as follows:

Local a,b,c = 1,2,3

A,b,c are all local variables.

4. Logical operation

Copy Code code as follows:

And, or, not

In Lua, only false and nil are evaluated as false, and any other data is evaluated as true,0 and true
The operations of and and or are not true and false, but are related to its two operands.
A and B: returns A;a True if A is false returns B
A or B: returns a;a false returns B if A is true
Simulate a statement in C: x = a? B:c, in Lua, 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.

5. Relational operations

Copy Code code as follows:

< > <= >= = = ~=

The results returned by these operators are either false or true.
= = and ~= are two values, and Lua thinks they are different if two value types are different.
The LUA comparison numbers are based on the traditional numeric size, comparing the strings in alphabetical order, but the alphabetical order depends on the local environment.
If you use the relational operator to compare two table, only the two table is the same object to get the expected result, because the variable is just a reference to the Table object, like a pointer, which cannot directly compare the values that exist in the table.
Copy Code code as follows:

> t_a = [' www.jb51.net ', ' q Group: 14624678 ']
> t_b = [' www.jb51.net ', ' q Group: 14624678 ']
> If t_a = = T_b Then
> Print ("true")
> Else
> Print ("false")
> End
False

T_a and T_b are two completely different structures.

Copy Code code as follows:

> t_a = [' www.jb51.net ', ' 14624678 ']
> t_b = t_a
> If t_a = = T_b Then
> Print ("true")
> Else
> Print ("false")
> End
True

The t_b points to the same object t_a.

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.