Lua Learning Note 1, basic data types

Source: Internet
Author: User

1. Strings are connected using the. Such as

Print (123.. 44)

Output 12344

Print (' a '.. ' B ')

Output AB

Print (123.. 44) The time of the sentence. Space on both sides

2. Lua annotation Symbols

Single line--

MultiRow

--[[

Here is the comment

--]]

3. String blocks

stringblock=[[

This is a string

Block

]]

4. Null type nil equals NULL, if a variable is unassigned, its value is nil, and the nil value in the conditional statement is false, for example

If Nil Then

Print (' nil ')

Else

Print (' nil is false ')

End

Output

Nil is False

5. # Put in front of string to calculate string length

STR1 = "This is a string"

Print (#str1)

Output

16

6. Table data type

This type is interesting, representing an associative array, a bit like a dictionary structure, where numbers and strings can be indexed

a={}--Empty table

a["key"]= ' value '

A[10]=22

For k,v in pairs (a) do

Print (k ... ":"). V

End

Output

: value:33

Here Pairs () is a function that can be viewed with print (type pairs), not specified in the usage ... I'll see you back.

In addition, the default index for table is starting from 1

tbl = { "Apple" ,  "pear" ,  "Orange" ,  "grape" }< Span class= "KWD" >for Key, val in Pairs (tbl do< Span class= "PLN" > print ( "Key"  Key) end 
Output
Key 1
Key 2
Key 3
Key 4

Table has no fixed length, no pre-declared size, no initialized table is nil

7. Function type
This type is very cool, and matlab inside the function is very similar, first function can be similar to the application of the transfer, and then function can be directly passed as functions parameters

function Fibonaccia (n)
if n = = 0 or n==1 Then
Return 1
Else
Return Fibonaccia (n-1) +fibonaccia (n-2)
End
End
Print (Fibonaccia (10))
Fib=fibonaccia
Print (FIB (10))
function Funcpass (tab,fun)
For k,v in Pairs (tab) do
Print (Fun (v))
End
End
tab={1,2,3,4,5}
Funcpass (TAB,FIB)

Summary: LUA has 8 basic data types: Nil,number,string,table,function,boolean,thread,userdata

Lua Learning Note 1, 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.