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

Inserting differently sizing tables into a table.

Started by guesswhat123212, 30 July 2014 - 02:06 AM
guesswhat123212 #1
Posted 30 July 2014 - 04:06 AM
What I am trying to do.
I am making a dynamically learning bee breeding program using CC and openP and having some issues trying to get one of my nested tables to work correctly. I am wanting my table to look like something below

{result= {1={species1,species2},2={species1,species2}}}

the reason for the 1= and 2= is to have all the different combos for the same bee listed such as common which has many. (and not have multiple "results" as common as that will break what I am trying to do as well)

currently my table has came out in many different wrong ways but its always either looking like the below
{result={1={species1,species2,species3,species4,…}}}
or
{result={species1,species2}} <—- missing all the other combos and just picks two to stay.

in spoiler below is current code I am working with.
Spoilertesting = apiary.getBeeBreedingData()
for k,v in ipairs(testing) do
for k1,v1 in pairs(testing[k]) do
if k1 == "result" then
result = v1
elseif k1 == "allele1" then
species1 = v1
elseif k1 == "allele2" then
species2 = v1
end
end
if not breedingTable[result] then
breedingTable[result] = {}
end
table.insert(breedingTable[result])
table.insert(breedingTable[result],species1)
table.insert(breedingTable[result],species2)
– breedingTable[result][species1] = {}
– breedingTable[result][species2] = {}
end
link to pastebin of code, http://pastebin.com/tUN12yxj
any advice or better reading material (read quite a bit of the LUA reference manual) would be a great help to me.


p.s. yes I know someone already created a program to do this but I dont want to use it for two reasons.
1. its a static list and will not adapt as things change.
2. most importantly, I think using the whole of someone else's code takes the fun and challenge out of it (though I do read a lot of them to try and learn things)
flaghacker #2
Posted 30 July 2014 - 07:04 AM
What kind of table structure are you getting from getBeeBreedingData?

And just a tip:

for k, v in pairs(data) do
  for k1, v1 in pairs(v) do --#instead of pairs(data[k])
    --#code
  end
end
That's why there is that second return from pairs!
Edited on 30 July 2014 - 05:05 AM
hilburn #3
Posted 30 July 2014 - 10:10 AM
There is a slightly better way of doing this that you might be interested in (I've also made a bee automation system)


apiary=peripheral.wrap("left")
breeding={}
bees=apiary.getBeeBreedingData()
for i,j in pairs(bees) do
	if not breeding[j.result] then
		  breeding[j.result] = apiary.getBeeParents(j.result)
	end
end

Which will return the data in the format you want, though it will also dump out data such as the breeding chance and special conditions which you might not be interested in, but you can strip those fairly easily
Edited on 30 July 2014 - 08:11 AM
guesswhat123212 #4
Posted 30 July 2014 - 12:39 PM
just woke up, I will have to tryout both suggestions today after work, flaghacker, now that you say that I feel a bit special but hey live and learn.
flaghacker #5
Posted 30 July 2014 - 06:53 PM
just woke up, I will have to tryout both suggestions today after work, flaghacker, now that you say that I feel a bit special but hey live and learn.

What I said isn't a way to do what you want, but a tip about for loops.
guesswhat123212 #6
Posted 30 July 2014 - 11:49 PM
just woke up, I will have to tryout both suggestions today after work, flaghacker, now that you say that I feel a bit special but hey live and learn.

What I said isn't a way to do what you want, but a tip about for loops.

I am aware of that but I will always take tips on how to improve my code. just got home, going to revise my loops with your tip so its easier to read and then try what hilburn told me and see how it turns out.
guesswhat123212 #7
Posted 31 July 2014 - 12:02 AM
There is a slightly better way of doing this that you might be interested in (I've also made a bee automation system)


apiary=peripheral.wrap("left")
breeding={}
bees=apiary.getBeeBreedingData()
for i,j in pairs(bees) do
	if not breeding[j.result] then
		  breeding[j.result] = apiary.getBeeParents(j.result)
	end
end

Which will return the data in the format you want, though it will also dump out data such as the breeding chance and special conditions which you might not be interested in, but you can strip those fairly easily

just tried this way and this builds out a great table for me (because I dont care about all the other infomation) I did not even think about using getBeeParents on each entry to make life easier. However I do have a question about that piece of code .result, can you point me somewhere that will explain what this is so I know about it in the future?
hilburn #8
Posted 31 July 2014 - 01:04 AM
Sure thing.

Basically the getBeeBreedingData() method returns a table of tables, as you know from your code, those secondary tables have allele1/2 which are the "parent" bees, the result entry is the "child" of the mutation, they also have specialconditions as another table, for example the new year bee has a date requirement there and others have a biome requirement, the final entry is the mutation chance.

Basically what that code does is go through every single mutation available, check if the result or child bee is already stored, if not, stores all of the possible allele pairings, conditions and mutation chances for that child bee (this prevents you overwriting the common entry like 67 times)

And I just realised you might have actually been asking why .result returns the child bee. That's to do with how tables work:


local testTable={123456,super="hello",["duper"]="world"} --# you can define string keys to table values either as ["string"] or as just the string text (if it's 1 word) = whatever
print(testTable[1]) --#prints 123456, if you don't assign a key to a table entry, it is assigned the next numerical key, starting with 1. Numerical key values can be accessed with [key]
print(testTable["super"]) --# prints hello, you can read from string keys in the same way as numerical keys
print(testTable.duper) --#prints world, it's just another method of referencing the key
print(testTable[super]) --# returns an error as you are trying to access the value at key nil (the value of variable super, instead you want to do
local temp = "super"
print(testTable[temp]) --#prints hello as temp passes "super" to the table key, so it's exactly the same as the second one

Hope this helped a little bit, and sorry for wasting your time with it if you knew all of that and I just misunderstood your question