Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build the Lua of Openresrt

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to build the Lua of Openresrt". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is lua?

Lua is a lightweight and compact scripting language written in the standard C language and open in source code. It is designed to be embedded in the application so as to provide flexible extension and customization for the application.

Environment building

Download address for Linux and Mac computers: http://luajit.org/download.html, the installation commands are as follows:

Wget http://luajit.org/download/LuaJIT-2.1.0-beta1.tar.gztar-xvf LuaJIT-2.1.0-beta1.tar.gzcd LuaJIT-2.1.0-beta1makesudo make install

Students developing with IDEA can integrate the environment of Lua by installing plug-ins called EmmyLua. After installing plug-ins, the icon of Lua will appear in the right bar of Idea. Click on the icon and a window running Lua code will appear. It is recommended to use this plug-in to avoid the hassle of installing the Lua environment.

The first Lua program

After installing the environment, I used the form of the EmmyLua plug-in to give a simple explanation of the entry syntax of Lua.

Open the terminal of EmmyLua and enter:

Print ("hi you")

Press ctrl+enter, and the terminal displays:

Hi you

Lua basic data type

The basic data types of lua are nil, string, boolean, number, and function.

Nil Typ

Nil is similar to null in Java and represents a null value. The variable is assigned nil for the first time.

Local numprint (num) num=100print (num)

Terminal output:

Nil

one hundred

Number (digital)

The Number type is used to represent real numbers, similar to the double type in Java. You can use mathematical functions

Math.floor (rounding down) and math.ceil (rounding up) perform rounding operations.

Local order = 3.99local score = 98.01print (math.floor (order)) print (math.ceil (score))

Output:

three

ninety-nine

String string

There are three ways to represent strings in Lua:

1. Use a pair of matching single quotes. Example: 'hello'.

2. Use a pair of matching double quotes. Example: "abclua"

3. Strings can also be defined in a way enclosed in long parentheses (that is, [[]])

Ocal str1 = 'hello world'local str2 = "hello lua" local str3 = [["add\ name",' hello']] local str4 = [= [string have a [[]].] =] print (str1)-> output:hello worldprint (str2)-> output:hello luaprint (str3)-> output: "add\ name", 'hello'print (str4)-table (Table)

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

Local corp = {web = "www.google.com",-- index is string, key = "web",-- value = "www.google.com" telephone = "12345678",-- index is string staff = {"Jack", "Scott", "Gary"},-- index is string, value is also a table 100876,-- equivalent to [1] = 100876 The index is a number-- key = 1, value = 100876 100191,-- equivalent to [2] = 100191 The index is the number [10] = 360 -- directly give the numeric index ["city"] = "Beijing"-- the index is a string} print (corp.web)-- > output:www.google.comprint (corp ["telephone"])-- > output:12345678print (corp [2])-- > output:100191print (corp ["city"])-- > output: "Beijing" print (corp.staff [1])-- > output:Jackprint (corp [10])-- > output:36function (function)

In Lua, a function is also a data type. A function can be stored in a variable and passed to other functions through parameters.

Number, which can also be used as a return value for other functions.

Local function foo () print ("in the function")-- dosomething () local x = 10 local y = 20 return x + yendlocal a = foo-- assign the function to the variable print (a ())-- output:in the function30 expression

~ = not equal to

Logic operators indicate that and logic is not the same as or logic or not logic

An and b returns an if an is nil, otherwise it returns b

An or b returns b if an is nil, otherwise it returns a.

Local c = nillocal d = 0local e = 100print (c and d)-> print nilprint (c and e)-> print nilprint (d and e)-> print 100print (c or d)-> print 0print (c or e)-> print 100print (not c)-> print trueprint (not d)-> print false

Concatenate two strings in Lua, you can use the operator ".." (two dots).

Print ("Hello".. "World")-- > print Hello Worldprint (0.. 1)-- > print 01 control statement

Single if branching type

X = 10if x > 0 then print ("x is a positive number") end

Two branches of if-else type

X = 10if x > 0 then print ("x is a positive number") else print ("x is a non-positive number") end

Multi-branch if-elseif-else type:

Score = 90if score = = 100 then print ("Very goodwill your score is 100") elseif score > = 60 then print ("Congratulations, you have passed it,your score greater or equal to 60")-multiple elseifelse print ("Sorry, you do not pass the exam!") endfor control structures can be added here

Lua provides a set of traditional and compact control structures, including if for conditional judgment, while and repeat for iteration

And for, this chapter mainly introduces the use of for.

For digital type

There are two forms of for statements: numeric for (numeric for) and generic for (generic for).

The syntax of a numeric for is as follows:

For var = begin, finish, step do--bodyend

Example 1:

For I = 1,5 do print (I) end-- output:1 2 3 4 5

Example 2:

For I = 1,10,2 do print (I) end-- output:1 3 57 9for generics

The generic for loop iterates through all values through an iterator function:

Print all the values of array a local a = {"a", "b", "c", "d"} for I, v in ipairs (a) do print ("index:", I, "value:", v) end-- output:index: 1 value: aindex: 2 value: bindex: 3 value: cindex: 4 value: d "how to build Openresrt Lua", thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report