Jump to content




how do I look up an entry in a table?


7 replies to this topic

#1 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

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

#2 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

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 by Lupus590, 16 March 2020 - 05:02 PM.


#3 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

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

#4 Luca_S

  • Members
  • 407 posts
  • LocationGermany

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 by Luca_S, 17 March 2020 - 11:01 AM.


#5 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

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/l...ls/invUtils.lua

#6 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 17 March 2020 - 06:13 PM

not sure if any of it may help but thanks for the help anyway.

#7 valithor

  • Members
  • 1,053 posts

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 by valithor, 25 March 2020 - 02:35 AM.


#8 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 10 June 2020 - 06:20 PM

View Postvalithor, on 25 March 2020 - 02:33 AM, said:

-chjop-

this is a little late of a response but thanks! you reminded me of like 3 projeckts on the backburner >kek
Thanks man, good help!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users