Hello, someone would be so kind to explain these table functions to me…
-- table.foreach()
-- table.foreachi()
-- table.sort()
I have learned them all except these
Hello, someone would be so kind to explain these table functions to me…
-- table.foreach()
-- table.foreachi()
-- table.sort()
I have learned them all except these
The wiki has a documentation for several libraries, including the table library. Just look for these functions in there.
I come from there and that is why it is my question, the rest I have already learned but those (the ones I mentioned) I do not understand
table.foreachi
allows you to run a function for every element in a Array. Ex.:
table.foreachi({"Hello", "World"}, function(Index, Value)
print(Index, Value); --> 1 Hello 2 World
end)
table.foreach
does the same thing, but it is instead used for Dictionaries.
table.foreach({
a = "Hello",
b = "World"
}, function(Index, Value)
print(Index, Value); --> a Hello b World
end)
table.sort
is used for performing sorting algorithms. You can provide a second argument, which will be used to determine whether to move element a
to element b
's place.
local unsortedTable = {10, 40, 50, 20, 30};
table.sort(unsortedTable);
print(unsortedTable) --> 10, 20, 30, 40, 50
But table.sort
performs its own algorithm, thus you can provide a second argument, a function (incomplete, please search more).
Wow thank you very much! You have helped me a lot, sorry for the inconvenience !!