How do I get first table value in Lua

Yurik

Is there an easier way to do this? I need to get the very first value in a table, whose indexes are integers but might not start at [1]. Thx!

local tbl = {[0]='a',[1]='b',[2]='c'}  -- arbitrary keys
local result = nil
for k,v in pairs(tbl) do -- might need to use ipairs() instead?
    result = v
    break
end
Colonel Thirty Two

If the table may start at either zero or one, but nothing else:

if tbl[0] ~= nil then
    return tbl[0]
else
    return tbl[1]
end

-- or if the table will never store false
return tbl[0] or tbl[1]

Otherwise, you have no choice but to iterate through the whole table with pairs, as the keys may no longer be stored in an array but rather in an unordered hash set:

local minKey = math.huge
for k in pairs(tbl) do
    minKey = math.min(k, minKey)
end

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

lua how to get table within table use variable value rather than variable as an index? TTS

分類Dev

How do I get the value of this void* back?

分類Dev

How do I get the value from this API?

分類Dev

How to check if a value is NOT anywhere in a Lua table?

分類Dev

how do i get last result in group by MySQL query (not the first)

分類Dev

How do I format values in my lua table to be: t = {['foo'] = true, ['bar'] = true}?

分類Dev

How Do You Get the Name For A Specific Value In A Powershell Hash Table?

分類Dev

How do I get LUA syntax highlighting inside of XML quotes in Sublime Text 3?

分類Dev

Lua)) how to loop table of table and get a specific property?

分類Dev

React - How do I display the first value of an array as the default value in a select form?

分類Dev

How do I get the value OUT of my maybe<> monad?

分類Dev

How do I get the rows where a certain value occurs?

分類Dev

How do I extract a GET value from a Http response?

分類Dev

How do i get the latest checked value of the checkbox?

分類Dev

How do I get the value of CharField() during instansiation?

分類Dev

How do I get a string value from service in an Angular component?

分類Dev

How do I get the counter clockwise value using the modulo operator?

分類Dev

How do I get the value from form input into a variable

分類Dev

MySQL - LEFT JOIN - How do it get the value that i need

分類Dev

How do I get chrome to use a time value in an html form?

分類Dev

How do I get return value of a JSON object as a variable

分類Dev

How can I get first 5 and last 1 records from table mysql?

分類Dev

How do I get only values in one table that are found in the second table (table1.a found in table2.a and so forth)?

分類Dev

How do I get a react-bootstrap-table2 caption to be above the table instead of below it?

分類Dev

How do I get code to read both first and last and values of a randomly generated array?

分類Dev

I have modified data in a datatable. How do I get this modified data back into the table?

分類Dev

Optic API: how do I get the value of a column that goes with a max value?

分類Dev

How do I set the value of multiple checkboxes that is linked to an associated table because of many-to-many relationship MVC

分類Dev

In SQL Server, how do I select IDs where value is in list, then insert matching IDs into join table?

Related 関連記事

  1. 1

    lua how to get table within table use variable value rather than variable as an index? TTS

  2. 2

    How do I get the value of this void* back?

  3. 3

    How do I get the value from this API?

  4. 4

    How to check if a value is NOT anywhere in a Lua table?

  5. 5

    how do i get last result in group by MySQL query (not the first)

  6. 6

    How do I format values in my lua table to be: t = {['foo'] = true, ['bar'] = true}?

  7. 7

    How Do You Get the Name For A Specific Value In A Powershell Hash Table?

  8. 8

    How do I get LUA syntax highlighting inside of XML quotes in Sublime Text 3?

  9. 9

    Lua)) how to loop table of table and get a specific property?

  10. 10

    React - How do I display the first value of an array as the default value in a select form?

  11. 11

    How do I get the value OUT of my maybe<> monad?

  12. 12

    How do I get the rows where a certain value occurs?

  13. 13

    How do I extract a GET value from a Http response?

  14. 14

    How do i get the latest checked value of the checkbox?

  15. 15

    How do I get the value of CharField() during instansiation?

  16. 16

    How do I get a string value from service in an Angular component?

  17. 17

    How do I get the counter clockwise value using the modulo operator?

  18. 18

    How do I get the value from form input into a variable

  19. 19

    MySQL - LEFT JOIN - How do it get the value that i need

  20. 20

    How do I get chrome to use a time value in an html form?

  21. 21

    How do I get return value of a JSON object as a variable

  22. 22

    How can I get first 5 and last 1 records from table mysql?

  23. 23

    How do I get only values in one table that are found in the second table (table1.a found in table2.a and so forth)?

  24. 24

    How do I get a react-bootstrap-table2 caption to be above the table instead of below it?

  25. 25

    How do I get code to read both first and last and values of a randomly generated array?

  26. 26

    I have modified data in a datatable. How do I get this modified data back into the table?

  27. 27

    Optic API: how do I get the value of a column that goes with a max value?

  28. 28

    How do I set the value of multiple checkboxes that is linked to an associated table because of many-to-many relationship MVC

  29. 29

    In SQL Server, how do I select IDs where value is in list, then insert matching IDs into join table?

ホットタグ

アーカイブ