Jump to content




Table Help

peripheral lua help

2 replies to this topic

#1 Cycomantis

  • Members
  • 60 posts

Posted 21 July 2014 - 02:37 AM

I am working with OpenCCSensors for my first time and I'm having a problem access the data for my BuildCraft Tanks.

Here is an example of the table that I'm having problems with:
Spoiler

How do I access the values stored in the "Tanks" array? I have never worked with a table thats formatted table.index = { { k = v } }

Here is the solution I've found for now. Can anyone improve on it? I'm not to fond of the nested loops.
local function getDetails()
	tDetails = {}
	for _,targetName in pairs(tTargets) do
		results = sens.getTargetDetails(targetName)
		tTanks = results.Tanks
		for _,tank in pairs(tTanks) do
			for k,dets in pairs(tank) do
				if tostring(k) == "Name" then tankName = tostring(dets)
				elseif tostring(k) == "Amount" then sAmount = tostring(dets)
				elseif tostring(k) == "Capacity" then sCapacity = tostring(dets)
				end
			end
			if tankName then
				if tankName == "jet fuel" then tankName = "jet_fuel" end
				tDetails[tankName] = { ["Amount"] = sAmount, ["Capacity"] = sCapacity }
			end
		end
	end
	return tDetails
end

Edited by Cycomantis, 21 July 2014 - 04:00 AM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 21 July 2014 - 04:29 AM

By looking at the structure, we can determine the direct references like so:

Spoiler

Now, with table indexes that are in the form of strings, there's no need to use the brackets to reference them - you can use the dot notation instead. Example:

Spoiler

So, with all that in mind, you should be able to re-write your function like this:

Spoiler


#3 Cycomantis

  • Members
  • 60 posts

Posted 21 July 2014 - 04:39 AM

View PostBomb Bloke, on 21 July 2014 - 04:29 AM, said:

By looking at the structure, we can determine the direct references like so:

Spoiler

Now, with table indexes that are in the form of strings, there's no need to use the brackets to reference them - you can use the dot notation instead. Example:

Spoiler

So, with all that in mind, you should be able to re-write your function like this:

Spoiler

Exactly what I was looking for. Thank you very much!!!!

I ended up with the following to record the used capacity for each of the different tanks I have setup.
Spoiler

Edited by Cycomantis, 21 July 2014 - 04:50 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users