This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
qwerty's profile picture

how do I look up an entry in a table?

Started by qwerty, 16 March 2020 - 03:04 PM
qwerty #1
Posted 16 March 2020 - 04:04 PM
How do I search a table for specific keys? I have read the 5.1 reference manual, but there's just too much to keep in track of.

specifically, I need to copy sub-tables from peripheral.getNames() to a different table.
since I'm making an inventory management program I need to keep track of where items are and since iterating trough a table is easier than polluting the global environment or hard coding things.

thanks in advance!

with regards,
-qwerty
Lupus590 #2
Posted 16 March 2020 - 04:55 PM
I'm not entirely sure what you are asking, but I hope this helps.

local t = {}
t.k = "example"
t[1] = "one"

local key = "k"
print(t[key]) --# -> example

key = 1
print(t[key]) --# -> one

Also, you might get answers faster if you post them on the discord: https://discord.gg/H2UyJXe
Edited on 16 March 2020 - 04:02 PM
qwerty #3
Posted 17 March 2020 - 08:54 AM
unfortunately, this is not what I meant.

so you know how string.match('string', 'pattern') returns the first capture of 'pattern' in 'string'? yeah, I kinda need that functionality.

so I have a table right? inside the table are tables referring to inventory slots, and within those are keys referring to items within; kind of like this:

{
  [3] = {
    count = 32,
    name = "minecraft:cobblestone",
    damage = 0,
  },
}

and what I need is to be able to efficiently search the table for the name and its appropriate key so that I have something like a function that accepts a string and then returns the exact slot and inventory it is in, all without having to manually add inventories through hard-coding them or polluting the global enviornment
Luca_S #4
Posted 17 March 2020 - 11:01 AM
Maybe something like this:

local t = {
  [3] = {
	count = 32,
	name = "minecraft:cobblestone",
	damage = 0
  }
}

local res
for k, v in pairs(t) do
  if v.name == "minecraft:cobblestone" then
	res = k
	break
  end
end
print("cobblestone goes into slot " .. res)

EDIT: Be aware that this will only find one result of course, also iirc pairs is not deterministic, so if you have multiple minecraft:cobblestone it might find a random one each time
Edited on 17 March 2020 - 10:01 AM
Lupus590 #5
Posted 17 March 2020 - 11:43 AM
Shameless plug: sounds like you will find my invUtils useful, be warned that I have yet to test this code so it's not guaranteed to work: https://github.com/lupus590/CC-TreeFarm/blob/master/treeFarm/libs/utils/invUtils.lua
qwerty #6
Posted 17 March 2020 - 06:13 PM
not sure if any of it may help but thanks for the help anyway.
valithor #7
Posted 25 March 2020 - 02:33 AM
Basically it sounds like you want to take a table and build a lookup table based upon the information.

Given a table of structure:

local tbl = {
  [3] = {
	count = 32,
	name = "minecraft:cobblestone",
	damage = 0,
  },[7] = {
	count = 32,
	name = "minecraft:stone",
	damage = 0,
  },[10] = {
	count = 13,
	name = "minecraft:cobblestone",
	damage = 0,
  },
}

You could do something like this:

local lookupTbl = {}
for k,v in pairs(tbl) do --# look through each index in the table.  k will be the key, v will be the value
  if not lookupTbl[v.name] then --# check to see if we have information about the item in our lookup table already
	lookupTbl[v.name] = {} --# we haven't seen the item before so initialize the table to hold the item
  end

  table.insert(lookupTbl[v.name],{slot=k,amount=v.count} --# insert the slot and amount information into the table for fast lookup
end

This would build a table that looks something like this:

{
  [minecraft:cobblestone] = {
	{
	  slot = 3,
	  amount = 32
	},
	{
	  slot = 10,
	  amount = 13
	}
  },
  ["minecraft:stone"] = {
	slot = 7,
	amount = 32
  }
}

Using this new table you can lookup all locations of each item instantly. It then becomes a problem of looping through all of the slots to figure out how much you have.


print(lookupTbl["minecraft:cobblestone"][1].slot) --# prints "3"
print(lookupTbl["minecraft:cobblestone"][1].amount) --# prints "32"
Edited on 25 March 2020 - 01:35 AM