Lua learns note 3. function variable arguments and operators, escape strings, arrays

Source: Internet
Author: User
Tags lua

1. LUA functions can accept variable-length arguments, similar to the C language, and are used in the parameter list of a function (...). Indicates that the function can accept variable-length parameters

The LUA function stores the parameters in a table, such as ARG, so that #arg can get the number of arguments

function Func_no_p (...)  Local arg={...}   for on Do print (v.. " , " End Print (" number of input parameters:"). #arg) endfunc_no_p (1,2,,1,"string " ")

2. Operators need to be aware that the inequality symbol is the same as Matlab ~=, as well as the precedence of various operators

^ Not-(unary)-- here is the symbol */+-.           <        >        <=        >=        ~=        = =Andor

except ^ and. All of the two-dollar operators are left-contiguous.

The special note here is that x^y^2 is equivalent to x^ (y^2)

3. Escaping a string

Escape character Significance ASCII code
\a Bell 007
\b BACKSPACE (BS) to move the current position to the previous column 008
\f Page Break (FF), moving the current position to the beginning of the next page 012
\ n Line Break (LF) 010
\ r Enter (CR) 013
\ t Horizontal tab 009
\v Vertical tabulation 011
\\ \ 092
\‘ 039
\" " 034
/ Null character 000
\ddd Octal Three-bit octal
\xhh Hexadecimal Two-bit hexadecimal

4. Some important operations of the string

String.upper (argument)

String.Lower (argument)

String.gsub (Mainstring,findstring,replacestring,num)--string substitution

Where Num represents the number of times to be replaced, if NUM is greater than or equal to the number of fingstring included, or num omitted is all replaced, the former replaces num

> string.gsub ("Hello", ' l ', ' ee ')

Output HEEEEEO 2, two values

String.find (Str,substr,[init,[end])

Where [] is an optional parameter representing the search interval, returning two values indicating that the substring starts in the original string and terminates the index

> String.find ("Lua is AA new Wapon", ' EA ', 5,10)

Output 16 17

String.reverse (ARG)

String.Format (...)

Formatted string, such as String.Format (' The value is%7.0d ', 4) output occupies 7 bits of 4

String.char (ARG), String.byte (Arg[,int])

Converts an integer number to a string connection, converting the first character in the string arg or an int specified character to an integer value

> String.char (97,65,48) Output aA0

>string.byte ("Hello") output 72

String.len (ARG) and #arg the same function, statistic the length of ARG

String.rep (string,n) copy n-Times string

.. Connection of strings

5. Lua arrays

One-dimensional and multidimensional arrays are table in Lua, except that each index of a table in a multidimensional array corresponds to table

It is important to note that the table index can set its own value, so it is not qualified to count from 0 or 1, but it is counted from 1 by default.

Returns nil if a non-existent index is used or if the index range is exceeded

Two-dimensional array instances

Arr={} forI=1,3  DoArr[i]={}     forj=1,3  DoA[i][j]=i*J EndEnd forI=1,3  Do     forj=1,3  Doprint (arr[i][j]) endend--another index for ARR={}maxrows=3Maxcols=3 forrow=1, MaxRows Do     forCol=1, Maxcols DoArr[row*maxcols+col]=row*col--Note that this is not a EndEnd index starting at 0 or 1. forrow=1, MaxRows Do     forCol=1, Maxcols DoPrint (Arr[row*maxcols+Col]) EndEnd

6. iterators

Iterators include stateless iterators and multi-state iterators, which are typically composed of iterative functions, ' state constants ' and ' control variables ', where state constants and control constants are input parameters of an iterative function

Iterative functions often output two values: the control parameter and the function value under the control parameter, such as the iterator we define as follows

function Tabsearch (TAB1, curidx)     = #tab1    if curidx<maxCount then        curidx=curidx+1        return  Curidx, Tab1[curidx]    endendtab1={1,2,3,4,  5} for in Tabsearch, TAB1,0do    print (v) end    

Ipairs Implementation method

function iter (a,i)    i=i+1    Local v=A[i]    if  v        Then  return  i,v    endendfunction ipairs (a)    return0End 

In fact, the difference between stateless and multi-state iterators is not big, the parameters of a stateless iterator are only state constants and control variables, like the maximum length and index in an array, and a multi-state iterator needs to pass many parameters in the iteration function, so you can use the array as the state constant input, As the Tabsearch iteration function above

7. Table operation (first LUA has garbage auto-recycle mechanism)

Table.concat (Table,[,sep [, start [, end]]): Where Sep defines the delimiter for the connection, Start,end defines the range of the table in the back of the connection

Table.insert (Table,[pos,] value])

Table.remove (Table[,pos])

Table.sort (Table[,comp]): In ascending order of the specified table


days={'Mon',"Tue","Wed","Thu","Fri"}string1="before sorting:" forKvinchIpairs (days) Dostring1=string1. V.." "EndPrint (string1) table.sort (days) string2="after sorting:" forKvinchPairs (days) Dostring2=string2. V.." "EndPrint (string2)

Lua learns note 3. function variable arguments and operators, escape strings, arrays

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: 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.