Lua learns----LUA basic data types

Source: Internet
Author: User
Tags lua

Objective

LUA has 6 data types, nil (empty), Boolean (Boolean), number (numeric), string (character), table (table), function (functions)

You can use the type function in Lua to return a value or the type to which a variable belongs, such as:

Print (Type ("Helle World"))-->output:stringprint (type (print))-->output:functionprint (type (TRUE))-- Output:boolean
1. Nil (empty)

Lua uses nil to denote an "invalid value." The default value is nil before the first assignment of a variable, and the nil assignment to a global variable is equivalent to deleting it.

Local numprint (num)-->output:nilnum = 100print (num)-->output:100
num = nil print (num)-->output:nil
2. Boolean (Boolean)

The optional Boolean value is True/false, nil and false in Lua are false, and the rest is true, such as 0 and null characters are true.

3. Number (numeric)

Number is used to represent real numbers, you can use the Math function Math.floor (rounding down), Math.ceil (rounding up)

Localorder=3.0localscore=98.5print (Math.floor (order))-->output:3print (Math.ceil (score))-->output:99
4. String (character)

In Lua, there are three ways to represent strings:

1) Use a pair of single quotes. such as: ' Hello '

2) Use a pair of double quotation marks. such as: "Hello"

3) Use braces (that is, [[]]) to define.

Note: The transfer character in Lua's string does not work.

The LUA string is a non-modifiable value and cannot be modified as a character of a string directly in the C language, but instead creates a new string based on the modification requirement.

LUA also cannot access a character of a string by subscript.

LOCALSTR3 = [["Add\name", ' hello ']]LOCALSTR4 = [=[stringhavea[[]]. =]print (STR1)-->output:helloworldprint (str2)-->output:helloluaprint (STR3)-->output: "Add\name", ' Hello ' Print (STR4)-->output:string has a [[]].
5. Table (Tables)

The table type implements an abstract "associative array". An associative array is an array that has a special index, which is usually a string or number type, but can also be a value of any type except nil.

6. function (Functions)

In Lua, a function is also a data type that can be stored in a variable, passed to other functions by argument, and can be used as a return value for other functions.

The function ends with end.

Local function foo () print ("In the Function")--dosomething () Local x = ten local y = return x + yendlocal a = foo- -Assign the function to the variable print (A ())--output:in the Function30

  

Lua learns----LUA basic data types

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.