Untitled
unknown
lua
4 years ago
2.6 kB
8
Indexable
--[[
Tables:
represent arrays,lists,records and keys and many more
Table: container for mutlipule Variables
Examples]]
-- instead you can use Tables
--local A = 5
--local B = 8
--local C = 10
--local D = 15
-- Indexs lua starts at 1
-- 1 2 3 4
local TheTable = {5,8,10,15}
-- You can not print Tables
-- You can store anydata type value inside of a table
---------------------------------------------------------------------------------
-- Getting Values inside a table
print(TheTable[4]) -- prints 15 because 15 is in index 4
---------------------------------------------------------------------------------
-- Getting the Length of a table
print(#TheTable) -- outputs 4 because there's 4 items in the table
---------------------------------------------------------------------------------
-- Sorting a table
table.sort(TheTable)
print(TheTable[1]) -- you will recieve 5 because 5 is the smallest value in the array
---------------------------------------------------------------------------------
-- Looping through a table
-- This is how we can get all the values of the table
for i = 1, #TheTable do
print(TheTable[i])
end
---------------------------------------------------------------------------------
-- Inserting into a table
-- you can use Table.insert
table.insert(TheTable, 2 ,"Hey") -- there's 3 arguements The table, The index, and what you want to insert in the table.
---------------------------------------------------------------------------------
-- Removing Values from a table using Table.remove
table.remove(TheTable, 2, 10) -- table,index, and what you want to delete
---------------------------------------------------------------------------------
-- Concating a table
local Table_concat = {"Hello," "There," "I'm," "A," "Table," "."}
table.concat(table.concat(Table_concat, " ")) -- two arguements the table and how you want to concat it
---------------------------------------------------------------------------------
-- Creating a multi demisonal table
local multi_Demisonal_Table = {
{1,2,3},
{4,5,6},
{7,8,9}
}
---------------------------------------------------------------------------------
-- Printing a multi_Demisonal_Table
print(array[2][1]) -- acessing the first table inside the table
---------------------------------------------------------------------------------
-- Looping through a multi_Demisonal_Table
-- for loop inside of a foor loop
for i = 1, #multi_Demisonal_Table do
print(multi_Demisonal_Table[i])
for j = 1, #multi_Demisonal_Table[i] do
print(multi_Demisonal_Table[i][j])
end
end
Editor is loading...