Table functions i need a hand

Hello, someone would be so kind to explain these table functions to me… :pray:

-- table.foreach()
-- table.foreachi()
-- table.sort()

I have learned them all except these :frowning:

The wiki has a documentation for several libraries, including the table library. Just look for these functions in there.

1 Like

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

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

1 Like

Wow thank you very much! You have helped me a lot, sorry for the inconvenience :smiley: !!