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

[Lua][Tables] i know there must be a better way to do it

Started by LordIkol, 01 April 2013 - 07:46 AM
LordIkol #1
Posted 01 April 2013 - 09:46 AM
Hi everybody,

im working on a script where i need to store data like "amount" "x" "y" "z" to a numeric table.

i made this code


retrievers = 10

function InitialSpots()
for i = 1, retrievers do
retSpots[i] = {}
retSpots[i]["amount"] = {}
for k = 1, retrievers do
retSpots[i].amount[1] = 0
end
end
[size=4]end[/size] 


now i can adress the amount of field 5 with
retSpots[5].amount[1]

i think there is a way to create it so that i can just use :
retSpots[5].amount

Thankfull for any Tipp
remiX #2
Posted 01 April 2013 - 09:51 AM
You're making the amount into a whole table itself, which isn't needed

local retrievers = 10
local redSpots = {} -- you need to declare it first if you havent

function InitialSpots()
	for i = 1, retrievers do
		retSpots[i] = {}
		retSpots[i]["amount"] = 0
	end
end
LordIkol #3
Posted 01 April 2013 - 10:02 AM
Ah thanks Remixi,
thats what i was searching for.
Should have asked earlier :D/>

i had nearly the same code than you in the beginning just missing the
 retSpots[i] = {}
in the loop and got error and by searching on google i came to the code that i posted before :D/>

anyway big thanks for the help
LordIkol #4
Posted 01 April 2013 - 11:26 AM
Hi another question about making Code better :D/>



function Rgetlowspot()
local high = 999
local temp = 999
for k,v in pairs(retSpots) do
if high > v.amount then
high = v.amount
temp = k
end
end
return temp
end

I want to know which ID in table retSpots has the lowest amount so i made this function.
is there a way to pass amount as argument to the function?

i tried many ways but it did not work.
want to be able to call this function like Rgetlowspot(fuel) Rgetlowspot(amount) etc…

if you have any advice or a good tutorial for all the tables stuff i would be happy :)/>
Thanks in advance
Loki
remiX #5
Posted 01 April 2013 - 11:36 AM
Get the lowest of a table? Not sure how you will get the lowest fuel?
Not sure what you're asking for.

But this code will get the lowest amount in any table that has the amount key
local retrievers = 10
local retSpots = {} -- you need to declare it first if you havent

local function InitialSpots()
	for i = 1, retrievers do
		retSpots[i] = {}
		retSpots[i]["amount"] = math.random( 1, 99 ) -- test
	end
end

local function getLowestAmount( tab )
	local index, lowest = nil, math.huge
	for i, v in pairs( tab ) do
		if v.amount < lowest then lowest = v.amount index = i end
	end
	return index, lowest
end

InitialSpots()
term.clear()
term.setCursorPos( 1, 1 )
for i, v in pairs( retSpots ) do
	print( v.amount )
end

local index, amount = getLowestAmount( retSpots )
print( '\nIndex: ' .. index .. ' | Amount: ' .. amount )
LordIkol #6
Posted 01 April 2013 - 12:06 PM
Sorry i did not explain this correct

lets say my table looks like this



function InitialSpots()
for i = 1, retrievers do
retSpots[i] = {}
retSpots[i]["amount"] = 0
retSpots[i]["x"] = 0
retSpots[i]["y"] = 0
retSpots[i]["z"] = 0
retSpots[i]["f"] = 0
end
end

retSpots[1]["amount"] = 5
retSpots[2]["amount"] = 2
retSpots[3]["amount"] = 8
retSpots[4]["amount"] = 51


now the function i made works fine when i run it i get 2 as result cause thats the index of retSpots with the lowest value for the key "amount".

but the way it is at the moment i can only check for the "amount" entry in the table but not get the lowest "x" entry. It would be nice when i could pass a variable to the function with the key i want to get the lowest value from

sorry for bad explenation its not my natural language but i hope this helps to get what i mean.
Kingdaro #7
Posted 01 April 2013 - 01:56 PM
This should fit your needs nicely. It's basically remiX's method, but with a key implemented.

function lowestOf(tab, key)
  local lowest
  for i=1, #tab do
    if not lowest or tab[i][key] < tab[lowest][key] then
      lowest = i
    end
  end
  return lowest
end

For the record, you can do this:

retSpots[i] = {}
retSpots[i].x = 0
retSpots[i].y = 0
retSpots[i].z = 0
retSpots[i].f = 0

Even better:

retSpots[i] = {x = 0, y = 0, z = 0, f = 0}

Way less typing in the longrun. Remember that when using these methods, you can still access them in both ways.

print(retSpots[2]["x"]) --> whatever the x value of the second element is
print(retSpots[2].x)    --> same thing

str = 'x'
print(retSpots[2][str]) --> same thing

Also note that brackets are needed for numbers, so this won't work:

retSpots[i].1 = h
LordIkol #8
Posted 01 April 2013 - 02:18 PM
Awsome Boys!

As always thank you for the help and especially for the good tipps Kingdaro.
Helps me to understand the Tables in lua a bit better.

Thanks for now and hope to read from you again when i have the next Question ;D