Lua Table Integer Index attribute

Source: Internet
Author: User

TABLE.MAXN (table)

Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To-do it job this function does a linear traversal of the whole table.)

Returns the largest positive value index in the table.

Description

1. This interface is not the number of elements in the statistics table.

2, if the table positive numerical index is not contiguous, 12 4, 3 is missing the element of index, the calculated value is MAXN 4.

LocalTest = {1}Table.insert(test, {})Table.insert(Test,4, {})Print("TABLE.MAXN (test) ="..TABLE.MAXN(test)) forI,vinch Pairs(test) Do    Print("i=".. I.."v="..ToString(v))End

LOG:

>lua-e "Io.stdout:setvbuf ' No '" "Luatest.lua"
TABLE.MAXN (test) =4
I=1 v=1
i=2 v=table:00559670
I=4 v=table:005597b0

For Ipairs vs Non Positive Integer Index

For discontinuous positive integer index cases, you can only get the first contiguous index segment if you use a ipairs iterator.

Therefore, you cannot traverse all elements and use the pairs iterator if necessary.

LocalTest = {1}Table.insert(test, {})Table.insert(Test,4, {})Print("TABLE.MAXN (test) ="..TABLE.MAXN(test)) forI,vinch Ipairs(test) Do    Print("i=".. I.."v="..ToString(v))End

LOG:

>lua-e "Io.stdout:setvbuf ' No '" "Luatest.lua"
TABLE.MAXN (test) =4
I=1 v=1
i=2 v=table:00339580

Table.remove (table [, POS])

Removes from table the element at position pos , shifting down and elements to close the space, if necessary. Returns the value of the removed element. The default value pos n for IS, where are the n length of the table, so, a call table.remove(t) removes the last Elemen T of table t .

Delete the pos position element and move the left high index element one position to the left. The high index referred to here refers to all elements on the left, so the MAXN value is reduced by 1.

LocalTest = {1}Table.insert(test, {})Table.insert(Test,4, {})Print("TABLE.MAXN (test) ="..TABLE.MAXN(test)) forI,vinch Pairs(test) Do    Print("i=".. I.."v="..ToString(v))EndTable.remove(Test,1)Print("TABLE.MAXN (test) ="..TABLE.MAXN(test)) forI,vinch Pairs(test) Do    Print("i=".. I.."v="..ToString(v))End

LOG:

>lua-e "Io.stdout:setvbuf ' No '" "Luatest.lua"
TABLE.MAXN (test) =4
I=1 v=1
i=2 v=table:00589350
I=4 v=table:005892b0
TABLE.MAXN (test) =3
I=1 v=table:00589350
I=3 v=table:005892b0
>exit code:0

Table.insert (table, [POS,] value)

Inserts element at value position pos table in, shifting up and other elements to open space, if necessary. The default value pos n+1 for IS, where are the n length of the table (see§2.5.5), so, and a call table.insert(t,x) inserts at the end of table t .

Inserts the value of the element into the POS position, and this position and the above elements move one bit to the right. The MAXN value is added to 1.

LocalTest = {1}Table.insert(test, {})Table.insert(Test,4, {})Print("TABLE.MAXN (test) ="..TABLE.MAXN(test)) forI,vinch Pairs(test) Do    Print("i=".. I.."v="..ToString(v))EndTable.insert(Test,1, -)Print("TABLE.MAXN (test) ="..TABLE.MAXN(test)) forI,vinch Pairs(test) Do    Print("i=".. I.."v="..ToString(v))End

LOG:

>lua-e "Io.stdout:setvbuf ' No '" "Luatest.lua"
TABLE.MAXN (test) =4
I=1 v=1
i=2 v=table:00979800
I=4 V=table:009795f8
TABLE.MAXN (test) =5
I=1 v=33
i=2 v=1
I=3 v=table:00979800
I=5 V=table:009795f8
>exit code:0

Table.remove vs Nil Set

Nil does not move the right element to the left by one bit, remove.

LocalTest = {1}Table.insert(test, {})Table.insert(Test,4, {})Print("TABLE.MAXN (test) ="..TABLE.MAXN(test)) forI,vinch Pairs(test) Do    Print("i=".. I.."v="..ToString(v))Endtest[1] =NilPrint("TABLE.MAXN (test) ="..TABLE.MAXN(test)) forI,vinch Pairs(test) Do    Print("i=".. I.."v="..ToString(v))End

LOG:

>lua-e "Io.stdout:setvbuf ' No '" "Luatest.lua"
TABLE.MAXN (test) =4
I=1 v=1
i=2 v=table:003d9508
I=4 v=table:003d9580
TABLE.MAXN (test) =4
i=2 v=table:003d9508
I=4 v=table:003d9580
>exit code:0

Lua Table Integer Index attribute

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.