generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Comparing tables

Previous Thread :: Next Thread 
DevUndead is not online. DevUndead
Joined: 24 Jul 2014
Total Posts: 558
29 Dec 2014 08:31 PM
a = {"a", "a"}
b = {"a", "a", "b", "b"}

How do you compare to see if the contents of the table a are also contained in table b and return true if so. In this case return true?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
29 Dec 2014 08:34 PM
So it doesn't matter what position, as long as it contains the same values anywhere?
Report Abuse
DevUndead is not online. DevUndead
Joined: 24 Jul 2014
Total Posts: 558
29 Dec 2014 08:36 PM
Right ^ just if a is container in b then true

How? :(
Report Abuse
iiEssence is not online. iiEssence
Joined: 18 Jun 2014
Total Posts: 3467
29 Dec 2014 08:38 PM
make a table function :D
Report Abuse
lordrambo is not online. lordrambo
Joined: 16 Jun 2009
Total Posts: 20628
29 Dec 2014 08:38 PM
If table b has all of the contents of table a?
There's probably a better way to accomplish whatever you're trying to accomplish.
Report Abuse
DevUndead is not online. DevUndead
Joined: 24 Jul 2014
Total Posts: 558
29 Dec 2014 08:39 PM
Well if you want the full thing, a is a crafting recipe and if b, the inventory; has same elements needed to complete the recipe then true.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
29 Dec 2014 08:41 PM
So basically: "if b contains a"
Report Abuse
DevUndead is not online. DevUndead
Joined: 24 Jul 2014
Total Posts: 558
29 Dec 2014 08:42 PM
Right
Report Abuse
lordrambo is not online. lordrambo
Joined: 16 Jun 2009
Total Posts: 20628
29 Dec 2014 08:44 PM
inventory = {}
recipe = {}

canCraft = true
for i, itemRec in pairs(recipe) do
for i, itemInv in pairs(inventory) do
if itemRec == itemInv then
break
end
end
canCraft = false
end

looks good to me
Report Abuse
DevUndead is not online. DevUndead
Joined: 24 Jul 2014
Total Posts: 558
29 Dec 2014 08:44 PM
How do I do it master cnt?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
29 Dec 2014 08:45 PM
Well I can think of a pretty good way but it requires values to be removed.
Not sure if this works but

local count = 0;
local required = #a;
for key = 1, #b do
local value = b[key];
for key2 = 1, #a do
if value == a[key2] then
table.remove(a, key2);
count = count + 1;
break
end
end
end

print(count == required);
Report Abuse
DevUndead is not online. DevUndead
Joined: 24 Jul 2014
Total Posts: 558
29 Dec 2014 08:48 PM
lord that doesn't work because it test if a is exactly equal to b (a == b) not if a is contained in b

Thanks tho luv u
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
29 Dec 2014 08:49 PM
Not to mention it would always be false :)
Report Abuse
DevUndead is not online. DevUndead
Joined: 24 Jul 2014
Total Posts: 558
29 Dec 2014 08:52 PM
This doesn't work how would you guys make a crafting system?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
29 Dec 2014 08:54 PM
Just tested it and it worked for all 3 of the cases that I've tried.
But now that you say it's a crafting system, there are better ways to do it since I hadn't known :)

table.sort might be your friend here
Report Abuse
lordrambo is not online. lordrambo
Joined: 16 Jun 2009
Total Posts: 20628
29 Dec 2014 08:55 PM
It always returns false because I was breaking the wrong loop. Oops. Try this instead, made it a function.

local inventory = {1, 2, 3}
local recipe = {1, 2, 3}

function craft(inventory, recipe)
for i, itemRec in pairs(recipe) do
local hasItem = false
for i, itemInv in pairs(inventory) do
if itemRec == itemInv then
hasItem = true
end
end
if not hasItem then
return false
end
end
return true
end

print(craft(inventory, recipe)) -->true
I don't know what you meant about earlier but I think you mistook something I said.
Report Abuse
DevUndead is not online. DevUndead
Joined: 24 Jul 2014
Total Posts: 558
29 Dec 2014 08:55 PM
Can you tell me the better ways masta
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
29 Dec 2014 09:00 PM
I also just tested the script I gave you and it worked :)

a = {'water', 'water', 'soap'};
b = {'soap', 'soap', 'soap', 'water', 'water', 'water'};

local count = 0;
local required = #a;
for key = 1, #b do
local value = b[key];
for key2 = 1, #a do
if value == a[key2] then
table.remove(a, key2);
count = count + 1;
break
end
end
end

print(count == required);

Report Abuse
lordrambo is not online. lordrambo
Joined: 16 Jun 2009
Total Posts: 20628
29 Dec 2014 09:01 PM
I may be mistaken but I do not think table.sort would be effective.

You could try this
inventory = {1, 2, 3}
recipe = {1, 2, 3}

inventoryString = table.concat(inventory, ", ")
for i, item in pairs(recipe) do
if !string.find(inventoryString, item) then
return false
end
end
inventoryString = nil
return true

Assuming there are strings in the inventory and recipe tables, and that you put it into a function.
Report Abuse
lordrambo is not online. lordrambo
Joined: 16 Jun 2009
Total Posts: 20628
29 Dec 2014 09:03 PM
Mine wont account for needing multiples of the same item; use cntkillme's if that's what you want.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
29 Dec 2014 09:04 PM
Yeah I thought about an idea but I no longer think it would be effective at all.
Report Abuse
DevUndead is not online. DevUndead
Joined: 24 Jul 2014
Total Posts: 558
29 Dec 2014 09:05 PM
Jesus... idea...


LETS ASK QUENTY yell his name he'll come
Report Abuse
iiEssence is not online. iiEssence
Joined: 18 Jun 2014
Total Posts: 3467
29 Dec 2014 09:06 PM
adding from cnt's script

if you don't want the values removed, you can make a table with the removed values and add it back

a = {'water', 'water', 'soap'};
b = {'soap', 'soap', 'soap', 'water', 'water', 'water'};
removed = {}

local count = 0;
local required = #a;
for key = 1, #b do
local value = b[key];
for key2 = 1, #a do
if value == a[key2] then
table.insert(removed, key2)
table.remove(a, key2);
count = count + 1;
break
end
end
end
for i,removed in pairs(removed) do
table.insert(a, removed)
end
removed = {}

print(count == required);
Report Abuse
iiEssence is not online. iiEssence
Joined: 18 Jun 2014
Total Posts: 3467
29 Dec 2014 09:07 PM
wow i never thought table.concat would be useful
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
29 Dec 2014 09:08 PM
Well instead of table.insert, you can do something like:

local c = {unpack(a)}
and use c instead of a
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image