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

How to Use Tables with if statements without going through every single comparation?

Asked by 5 years ago
Edited 5 years ago

Im trying to figure out if there is any way to compare multiple values to one value using if statements. right now the only way I know how to do this takes a long time and im wondering if i can shorten the amount of code I use.

local comparative = false
local table = {Box1,Box2,Box3,Box4,Box5}
for i = 1,#table do
if(NewBox.Name == table[i].Name)then
comparative = true
end
if(comparative == true)then
print("NewBox is the same as one of the old boxes")
else
print("NewBox is not the same as any of the boxes")
end

Here is the code that I don't know if it works or not.

if(slot.Name == "Box"..{1,2,3,4,5})then
print("slot is an equiping spot")
end

1 answer

Log in to vote
0
Answered by 5 years ago

This answer's to be used as a reference

Perhaps the answer to your question's to use a dictionary? A dictionary is a table with set indexes and values, vs arrays where they have numbers for their index (to put simply). An example for a dictionary would be.

local Boxes = { -- Dictionary example
    ["Box1"] = true; -- `Box1` is the index, and `true` is the value for said index
    ["Box2"] = true; -- etc
    ["Box3"] = true; -- etc
}

if Boxes[Box.Name] then -- Checks to see if the name's in the table, and if it is...
    print('Box found!') -- Print that there was! :D
else -- If not...
    print('No such box found!') -- Nothing! D:
end

Stuff touched on

  1. Dictionaries - As the dev hub states, "a set of key/value pairs".

If you have any questions, please let me know. :) I hope this helped! :D

0
Tyvm for accepting! ^^ TheeDeathCaster 2368 — 5y
Ad

Answer this question