# Question on tables

So, I’m just asking a simple question. The table below is sorting it by least to greatest however, is there a possibility I can sort it from least to greatest with all the numbers in the same print statement? So, it would print out, 0,1,2,7,8,9 in the output all together.

``````local newTable = {9,0,8,7,1,2}

table.sort(newTable)
print(newTable[1])
``````

Sorry if this is confusing, it was kind of hard to word what I was trying to explain.

Quick note. What is the difference between an array and a table. I’m trying to learn how to script and right now I’m more in the beginner side of things.

2 Likes

An array is something with numerical indexes in order:

For example dude = {‘yo’,‘col’}

Lua automaitically gives yo the index 1 and col the index2.

This can also be considred an array:

local du = {[1] = ‘np’, [2] = ‘s’}

Here we are manually giving numerical indexes in order without lua automatically doing it for us

Now a table in lua is something with indexes that can be anything, and doesn’t have to have numerical indexes in order.

For example:

mydude = {[‘lol’] = 'asdas, ['DUDE], = ‘bruh’}

or

mydude = {[workspace.Baseplate] = ‘WOW A BASEPLATE’}

Keep in mind when you make an index you have to put it around for brackets, except for string’s its a special case since you can do it that way or declare the index without a bracket, without the quotes for example

MyTable = {Cool = 'my", Nomadic = ‘me’}

The index Cool is actually a string but because of some special lua features we don’t have to do

[‘cool’] and can do: cool

Yah this is just the difference between an array and a table your side question.

1 Like

Wait, can both be considered a string?

print(‘Hi’)
print(“Hi”)

Are like both quotations the same?

2 Likes

Here’s a quick function I came up with which you can copy and paste into the start of your code and print a table using it whenever you need for debugging. I’ve also put your code at the bottom and and used the function to show you how it might work. Hope this helps.

``````local function printTable(prtTable) --when the function is called, whatever argument was put in is set to the prtTable. This is what stores tha table
local tableStr --create a new, empty string that will store the values later in the loop.
for i, val in ipairs(prtTable) do --loop through all elements in the prtTable using ipars (ipars uses the order it's in whereas pairs disregards the order)
if #tableStr == 0 then --Roblox will give an error if you try to concat with an empty string, so if the string's length is 0, we just set it to the first value.
tableStr = val
else --if there is already part of the string made, we will just add a comma and then the next value. you can use two dots (..) to concat two strings
tableStr = tableStr..", "..val
end
end
print(tableStr) --once the loop is done and they are all added, we can simply print the string created.
end

local newTable = {9,0,8,7,1,2}
table.sort(newTable)
printTable(newTable) --how you would use the function in this case.
``````

@0Shank has a good explanation of your side question of what the difference is between tables and arrays, so kudos to him for that.

2 Likes

Yah in lua you can make a string with

‘’

or

“”

2 Likes

So, what does the function exactly do? I’m about a week into scripting and don’t know about in pairs loop/pairs loop.

1 Like