Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How to order arrays | How to check if something is a boolean?

Asked by
IXLKIDDO 110
8 years ago

As of right now, my main problem is arrays. This is probably the 4th question I've asked about arrays, but problems keep on popping up. So first off, what I'm looking for is how to order arrays. As of right now my problem is it coming out of order. For example, this is the array:

local Random = {
    Another = {
        Blank1 = 1,
        Blank2 = 2,
        Blank3 = 3,
    Blank4 = 4,
    Blank5 = 5,
    },
}

Now, what I want to do is to put the values in a table from top to bottom, however this is not happening.

local values = {}
for i, v in pairs(array) do
    table.insert(values, v)
end
return unpacked(values)

What this outputs is 3 2 1 4 5. How could I fix this first problem?


Next thing I want to do is check if something is a boolean. Pretty simple question.

0
Good Question! I'm not exactly sure, maybe because im tired :P dragonkeeper467 453 — 8y
0
Why do you have a table inside of a table? in the first code block dragonkeeper467 453 — 8y
0
if type(something) == "boolean" then... grasheeno 70 — 8y

3 answers

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

pairs returns entries in no particular order.

If you want to get things out in a particular order, you have to use a list, and you have to use ipairs.

Right now, Random.Another is not a list / array. It's a dictionary/hashmap -- you aren't using the numbers 1, 2, ... as the keys, you're using text as keys.

Instead,

local list = {2, 3, 5, 8, 13, 25}

for i, v in ipairs(list) do
    print(v)
end

Again, you can't use pairs and expect a particular order.

If you use ipairs, you'll get the same list -- since table.insert adds to the end, and the last element you'll be adding will be the last.

To go in opposite order, say you want to insert at the front: table.insert(values, 1, v)

Boolean

You can check if x is a boolean by asking for it's type:

if type(x) == "boolean" then

Or, you could just check if it's either true or false:

if x == true or x == false then
    -- x is a boolean

A "clever" way to do it is to check that not not x is the same thing as x:

if not not x == x then
    -- x is a boolean
    -- This is really unclear. While neat, don't do this.
Ad
Log in to vote
3
Answered by
ImageLabel 1541 Moderation Voter
8 years ago

Sorting

Adding on to BlueTaslem's answer ..

You could use the sort function of the table library which automatically sorts your tables from least to greatest unless instructed otherwise. Here is an example of how it works, using your code.

local Random = {
    Another = {
        Blank1 = 1,
        Blank2 = 2,
        Blank3 = 3,
        Blank4 = 4,
        Blank5 = 5,
    },
}

local values = {} -- new table

for i, v in pairs(Random.Another) do
    table.insert(values, v)
end

print(unpack(values)) -- unsorted
table.sort(values) -- call sort function
print(unpack(values)) -- sorted
Log in to vote
0
Answered by 8 years ago

First off I'll address the boolean issue, as that's fairly easy. There's a type() method that will tell you what sort of thing something is by returning a string of what said thing is.

local myVariable = true
if type(myVariable) == "boolean" then
print("It's a boolean!")
end

As for your array issue, what do you mean by ordering them "top to bottom"? Are you storing numerical values in each table in your array, and you want to order the tables in your array in terms of highest to lowest content value?

Answer this question