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

Problem with filtering tables

Started by Impervium, 16 October 2012 - 06:35 PM
Impervium #1
Posted 16 October 2012 - 08:35 PM
I've made code to keep track of my nuclear reactor and my mfsu's on a monitor, that's all and fine.
Problem is, the code I made targets …say… a MFSU as a target, not filtering it out as an MFSU first.
So if I got the sensorprobe with like 240 targets due to alot of cables and stuff laying around (this is actually the case right now and it's a nightmare), then stuff is prone to go bad fast.
Meaning if I remove or add a cable or similar object, I can (and have…several times) screwed up the program.
You will obviously see the program ending itself, due to the fact that by removing or adding an object similar to the one placed before, the target I made in the code has changed place in the table.


I've tried to read up on some "find" and such for tables, but the examples seem to be very vague..
Can someone help me filter out the targets correctly?



ctrl = sensors.getController()
data = sensors.getSensors(ctrl)
mainSensor = data[1]
probes = sensors.getProbes(ctrl,mainSensor)
EnergyProbe = data[1]
targets = sensors.getAvailableTargetsforProbe(ctrl,mainSensor,EnergyProbe)
EnergyTarget1 = targets[105] -- See the problem here? Had search manually and count on the sensorprobe to 105 just to get a lock on the target.. in this case an MFSU between alot of cables..
data1 = sensors.getSensorReadingAsDict(ctrl,mainSensor,EnergyTarget1,EnergyProbe)

ChunLing #2
Posted 16 October 2012 - 09:24 PM
for i,v in pairs(t_table) do somefunctions() end is a basic structure for going through all the indexes and values of t_table (in pairs) and doing somefunctions. I have no knowledge of industrialcraft or CCsensors (as I've not used either), so can't help you with the specifics of how to identify an "MFSU" and mark it as not a target.

If you need to find things by their value (rather than by index) a lot, then you can build a reverse table that uses the values as indexes and holds the indexes as the targets. This works well if your table has a one to one relationship of values to indexes, but if not then it gets a little tricky. If you expect more than one index to contain identical values, then the reverse table will need a subtable under each index (which is the values from the other table) holding all the indexes that held that value in the other table. And I'm pretty sure I only 'understand' what I just wrote in the sense of knowing what I was trying to say, so I'll end here.
Impervium #3
Posted 16 October 2012 - 09:44 PM
for i,v in pairs(t_table) do somefunctions() end is a basic structure for going through all the indexes and values of t_table (in pairs) and doing somefunctions. I have no knowledge of industrialcraft or CCsensors (as I've not used either), so can't help you with the specifics of how to identify an "MFSU" and mark it as not a target.

If you need to find things by their value (rather than by index) a lot, then you can build a reverse table that uses the values as indexes and holds the indexes as the targets. This works well if your table has a one to one relationship of values to indexes, but if not then it gets a little tricky. If you expect more than one index to contain identical values, then the reverse table will need a subtable under each index (which is the values from the other table) holding all the indexes that held that value in the other table. And I'm pretty sure I only 'understand' what I just wrote in the sense of knowing what I was trying to say, so I'll end here.

Ok I might've mispoken here..

I have a table that goes (for instance) like this:
1. redstone reciever1
2. cable1
3. MFSU1
4. cable3
5. MFSU2

where I usually have to go and select a target like MFSU = target[3] making a variable refering to the MFSU1

Problem I encounter is when someone remove or place a block, the target I targeted changes place in the table like so.

1. cable1
2. MFSU1
3. cable3
4. MFSU2

Making my script refer to a target that is now "cable3" and will by that not be able to read any values that it intends to do, forcing the script to quit.

What I need is to make a table that filters out these like

1. MFSU1
2. MFSU2

so worst case scenario, someone could place another MFSU making it

1. MFSU3
2. MFSU1
3. MFSU2

but I would still be able to read values and not have to rewrite alot of stuff.
Understand?
ChunLing #4
Posted 17 October 2012 - 01:31 AM
Problem I encounter is when someone remove or place a block, the target I targeted changes place in the table like so.

Okay, why does that happen? What causes the table to be updated? Is this table part of CCsensors?

If the table is being generated and updated by outside code (meaning something you didn't write), then you definitely need to rebuild it under your own table so that you have control over it. You cannot simply "copy" the table, like saying new_table = existing_table, that will only make the identifier new_table point to existing_table. You need to traverse the table, copying it element by element into the new table. You can structure the new table to have subtables for each different type of entry in the old table, and then when you access it you'll only get the kind of entries you want.

You can easily rebuild new_table from existing_table whenever you need it updated, the pairs function is built into lua and quite efficient.