Technology Sharing

Lua Introduction (2) - Data Types

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Preface

This article is referenced from:Lua data types | Novice tutorial (runoob.com)

If you want to know more details, please check the link above:

Eight basic types

type - function to check data type:

test program:

  1. print(type("Hello world")) --> string
  2. print(type(10.4*3)) --> number
  3. print(type(print)) --> function
  4. print(type(type)) --> function
  5. print(type(true)) --> boolean
  6. print(type(nil)) --> nil
  7. print(type(type(X))) --> string
  8. print(type(Y)) -->nil
  9. print(type('Y')) -->string

Effect Demonstration

You can see that the type(Y) we output here is a nil type. Why? The following nil (empty) will demonstrate it to you.

nil

The nil type represents a type without any valid value. It has only one value - nil. For example, printing a variable without a value will output a nil value:

Uninitialized variable type - nil

As follows: The output types of the three variables that have not been assigned initial values ​​are all nil

nil deletes the key-value pair in the table

test program

  1. tab1 = { key1 = "val1", key2 = "val2",
  2. "val3","val4",key3 = "val5" }
  3. for k, v in pairs(tab1) do
  4. print(k .. " - " .. v)
  5. end
  6. print("=========直接输出=============")
  7. tab1.key1 = nil
  8. for k, v in pairs(tab1) do
  9. print(k .. " - " .. v)
  10. end
  11. print("============删除key1后==========")
  12. tab1.key3 = nil
  13. for k,v in pairs(tab1) do
  14. print(k.. " - "..v)
  15. end
  16. print("=========删除key3后============")

operation result

For global variables and tables, nil also has a "deletion" effect. Assigning a nil value to a global variable or a variable in a table is equivalent to deleting it.

nil should be enclosed in double quotes when comparing

type(X)==nil results infalseThe reason is that type(X) actually returns the string "nil", which is a string type:

boolean

The boolean type has only two optional values: true and false. Lua considers false and nil to be false, and all others to be true. The number 0 is also true:

test program

  1. print(type(true))
  2. print(type(false))
  3. print(type(nil))
  4. if false or nil then
  5. print("至少有一个是 true")
  6. else
  7. print("false 和 nil 都为 false")
  8. end
  9. if 0 then
  10. print("数字 0 是 true")
  11. else
  12. print("数字 0 为 false")
  13. end

Effect Demonstration

number


By default, Lua has only one number type - double (double precision) type (the default type can be modified in luaconf.h). The following writing methods are all considered as numbers.

string

"" '' -- represents a string


[[ ]] -- represents a block of strings
 

Numbers can be coerced into strings

When performing arithmetic operations on a numeric string, Lua will try to convert the numeric string into a number:

Use .. to concatenate strings

 

table

In Lua, table creation is done through "construction expression". The simplest construction expression is {}, which is used to create an empty table. You can also add some data to the table to initialize the table directly:

Initialization table

Modify the table based on the index (key)

A table in Lua is actually a"Associative Arrays"(associative arrays), the index of the array can be a number or a string.

Lua indexes start at 1 (not

The length of a table is not fixed. When new data is added, the length of the table will automatically increase. The value of an uninitialized table is nil.

function

In Lua, functions are considered "first-class values".

Functions can be stored in variables:

Passing parameters using lambda expressions (anonymous functions)


thread


In Lua, the main thread is the coroutine. It is similar to a thread and has its own independent stack, local variables and instruction pointer.
Can share global variables and most other things with other coroutines.

The difference between threads and coroutines: threads can run multiple times at the same time, but only one coroutine can run at any time, and a running coroutine will only be suspended when it is suspended.

Userdata (custom type)


Userdata is a user-defined data type used to represent a type created by an application or C/C++ language library.
You can store any C/C++ data of any data type (usually struct and pointer) into a Lua variable.