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

How to use Conditional Statements on Tables?

Asked by 4 years ago

I am trying to use a Conditional Statement on two tables. it checks if there contents are the same, here is a sample of my code:

local Table1 = { }
local Table2 = { }
if Table1 ~= Table2 then
print("The Content of both Tables is different")
end

However, the game keeps thinking the two Tables contents are different, is there anyway i can fix this, thanks

0
If you do print(Table1) and print(Table2) it won't print the same in the output. Spjureeedd 385 — 4y
0
I can only think of a somewhat complicated loop to find out if your table's contents are the same. But I'm sure someone will give you a better answer Spjureeedd 385 — 4y
1
You are trying to print if the hexadecimal address is the same for the tables which isn't true you can create a nested loop though. greatneil80 2647 — 4y
0
Wow... 3 answers... this is sad greatneil80 2647 — 4y

3 answers

Log in to vote
3
Answered by 4 years ago
Edited 4 years ago
local table1 = {"Two", "Strings"}
local table2 = {"Two", "Strings"}

local match = false
for i = 1, #table1 do
    match = false
    for ind, v in pairs(table2) do
        if table1[i] == v then
            match = true
            break
        end
    end
    if match == false then
        print("Does not match")
        break
    end
end

On the 2 first lines we declare our tables. Then we make a variable equal to false. Then we make a loop that will run an equal amount of times as there are values in table1. In the beginning of the loop we once again set the value of our variable, match to false. Then we make a second loop inside of our first one that will run an equal amount of times as there are values in table2. That loop will check if there is any value in table2 that matches the current value of table1. If it does, it will set the value of match to true to announce that there was a match. But if there wasn't any value that matched. Our first loop will break and print that the tables do not contain the same values.

0
too long, try my code iuclds 720 — 4y
1
There is really nothing wrong with this. You can't downvote just because you think your code is better Spjureeedd 385 — 4y
0
what's wrrong with this answer? why do u just randomly down vote? User#23252 26 — 4y
0
bepis NightmaareTsukuyomi 78 — 4y
View all comments (3 more)
0
pepsi Psudar 882 — 4y
0
Thanks for the help Lord_WitherAlt 206 — 4y
0
You're welcome Spjureeedd 385 — 4y
Ad
Log in to vote
3
Answered by 4 years ago
Edited by Ziffixture 4 years ago

well the thing is doing {} == {} will always be false.. the == operator checks to see if it's (table)operands are the same object, not wheather they have the same content..

so if i did, local a = {x = 10} and local b = {x = 10}, then print(a == b) will always print false..

however, if i did b = a then it print(a == b) will return true because the statement implies that a IS b..

to check whether or not 2 tables have the same content, you'd have to do something like the following.

function isSame(tableA, tableB)
    for index, value in pairs(tableB) do
        if(tableA[index] == value) then
            return true
        end
    end
    return true
end

but this might not work if the table passed to pairs has less items than the other table, and the items that it has match some of the few that the other one has

Log in to vote
1
Answered by
iuclds 720 Moderation Voter
4 years ago
local table1, table2 = {},{}
for i,v in ipairs(table1) do
    if table2[i] = table1[i] then
        print(v..' matches in table2')
    end
end

Its pretty basic. If table2 finds something in table1, it prints that value.

1
why do randomly downvote @Spjureeedd's answer? -_- User#23252 26 — 4y
0
I apreciate it Arsubia (but it was an answer) Spjureeedd 385 — 4y

Answer this question