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

Array / Subarray Referencing

Started by Noobular, 07 September 2015 - 07:17 PM
Noobular #1
Posted 07 September 2015 - 09:17 PM

local MachineArray = {
monitor = {peripheral.find("monitor"), "Monitor"}
reactor = {peripheral.find("BigReactors-Reactor"), "Reactor"}
}

I assume it's meant to be something more like…

local MachineArray = {
MachineArray[monitor] = {peripheral.find("monitor"), "Monitor"}
MachineArray[reactor] = {peripheral.find("BigReactors-Reactor"), "Reactor"}
}

And also how do i reference everything…
KingofGamesYami #2
Posted 07 September 2015 - 09:38 PM
Um, no… the first one is correct.


local MachineArray = {
monitor = {peripheral.find("monitor"), "Monitor"}
reactor = {peripheral.find("BigReactors-Reactor"), "Reactor"}
}
OR

local MachineArray = {}
MachineArray.monitor = {peripheral.find("monitor"), "Monitor"}
MachineArray.reactor = {peripheral.find("BigReactors-Reactor"), "Reactor"}

And referencing:


MachineArray.monitor[ 1 ] --#the peripheral
MachineArray.monitor[ 2 ] --# "Monitor"
Lupus590 #3
Posted 07 September 2015 - 09:38 PM
edit: :ph34r:/> 'd

the first is how to define tables (arrays), your second almost contains the answer for accessing the data:


local MachineArray = {
monitor = {peripheral.find("monitor"), "Monitor"}
reactor = {peripheral.find("BigReactors-Reactor"), "Reactor"}
}


MachineArray.monitor
--or
MachineArray["monitor"]

numberExample = {"one", "two", "three"}

numberExample[1] -- value is one

Edited on 07 September 2015 - 07:39 PM
Noobular #4
Posted 08 September 2015 - 08:50 AM
Thank you both for your responses… Now i'm having issues with this… actually working.


local MachinesArray = {}
MachinesArray[1] = {printName = "Reactor",scriptName = "BigReactors-Reactor",MacType = "Big_Reactors"}
MachinesArray[2] = {printName = "Harvester",scriptName = "harvester",MacType = "MineFactory_Reloaded"}

function findPeripherals() 
for i = 1, #MachinesArray do

for key,value in pairs(MachinesArray[i]) do
print("MName: "..MachinesArray[i].printName.."| SName: "..MachinesArray[i].scriptName.."| Mod:"..MachinesArray[i].MacType)
end

end 
end

So.. it's listing total number of entries (6) rather then i assumed it would do how many entries before sub tables(2) is there an easy way to do a loop specifically for the uhh… "starts" i went this to list only MachinesArray[1] and [2] but it list a total of 6 times because all the entries within the subtables.
Bomb Bloke #5
Posted 08 September 2015 - 09:16 AM
Ditch that "pairs" loop you've got nested there, but leave the "print" line you put inside it.
Noobular #6
Posted 08 September 2015 - 09:32 AM
Ditch that "pairs" loop you've got nested there, but leave the "print" line you put inside it.

Attempt to call table error… Not sure how exactly you meant to take out pairs.
Noobular #7
Posted 08 September 2015 - 10:10 AM
Never mind i re-read what you said lol it's good now Thank you!
Noobular #8
Posted 09 September 2015 - 09:34 AM
I was curious if this was even possible.. because currently it's not working but its also not giving me any errors or anything so it's still running but i didn't get any prints that any harvesters existed (there's some connected.) Currently if anyone remembers me messing with a factory overview thing i'm re making that and trying to make it more efficient so i can add macines easier using just 1 array rather then 18 places in the code to add in.

Noobular #9
Posted 09 September 2015 - 09:57 AM
I should point out it is running the function.
Bomb Bloke #10
Posted 09 September 2015 - 10:46 AM
Let's say H starts out as nil, or false, or whatever. When you define your MachinesArray table, and set MachinesArray[2].Acr to H, all that means is that MachinesArray[2].Acr will be whatever H was at the time - it doesn't mean that it'll become an automatically-updating pointer to H.

Which means that when you later set MachinesArray[2].Acr to the table you're filling with found peripherals, H will remain unaltered.

Now, if you set H to equal MachinesArray[2].Acr after setting MachinesArray[2].Acr to your table filled with peripherals, then they'd both contain a pointer to the one table… and changing the contents of that table would allow you to pick up those changes via H or MachinesArray[2].Acr. Bear in mind an empty table is still a table, though = your "if H then" check would always resolve as true, you'd want "if #H > 0 then"!
Noobular #11
Posted 09 September 2015 - 05:08 PM
Ahh ok , and yea that makes sense putting #H rather then just H … thanks!
Guess i'll be sticking to the old way to do this.
Lyqyd #12
Posted 09 September 2015 - 06:00 PM
Threads merged. Please stick to one topic for all questions about a given program or piece of code.