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

Finding a table item?

Started by WhamyKaBlamy, 05 January 2015 - 01:28 PM
WhamyKaBlamy #1
Posted 05 January 2015 - 02:28 PM
Hi guys,

This is my first post here and I just wanted to say thanks because you've helped out a ton in the past even if you aren't aware of it :)/>

So, my question: I just wanted to check if there was an equivalent function in Lua of searching a table for a value and returning the index number of it?

Sorry if that seems a very newbie question and has been asked before - I'm fairly new to Lua as a language and I couldn't see anything for it in the wiki, most of the functions seemed to be based upon the idea of a loop going through the index keys.

To put it in context - I'm making a music program for Tekkit currently I have a planned set up of (as a rough example):


table note ={"C","C","D","D","E","E","F","F","G#","F"}
table note_2 = {"A#",0,0,0,0,0,"A#2",0,0,0,0}
table note_3= {"A#2,0,0,0,0,0,0,0,0,0,0}
local Bundled_Left = 0
local Bundled_Back = 0
for i=1, #note do
	redstone.setBundledOutput("left",0)
	Bundled_Left = 0
	os.sleep(0.1)
	if note[i] == "F#" then
		 Bundled_Left = Bundled_Left + 1
	elseif note[i] == "G" then
		 Bundled_Left = Bundled_Left + 2
	elseif note[i] == "G#" then
		 Bundled_Left = Bundled_Left + 4
	end
	if i <= #note_2 then
		if note_2[i] == "F#" then
		   yada yada
		end
	end
end



And essentially what I'd like to be able to do is have a table that has all of the notes and their associated rednet cable numbers stored in a table so I could do something like:


local Bundled_Left_Note_Values = {"F#",1,"G",2,"G#",4,"A",8,etc,etc}
local Bundled_Left = 0
for i=1, #note do
	 Bundled_Left = Bundled_Left + Bundled_Left_Note_Values[TableValueFindingFunctionName(Bundled_Left_Note_Values , Note[i]) + 1]
end


Sorry if that was a bit convoluted, thank you for any help you can give guys :)/>
MKlegoman357 #2
Posted 05 January 2015 - 03:54 PM
Sadly, there is no default library and/or function to do this. But, this is quite easily doable yourself by, like you said, looping through all keys and checking the value. Another option is to use a lookup table, which you can either write yourself or generate it in-code:


local tableOfValuesToFind = {["test"] = "print 1", ["value"] = "print 2"}

local lookupTable = {["print 1"] = "test", ["print 2"] = "value"}

checking with a lookup table is easier and done like so:


local value = tableOfValuesToFind.test --# get a value of a table

local indexOfValue = lookupTable[value] --# get the index (key) of that value in a table

print( indexOfValue ) --> test

You can also generate one dynamically:


local lookupTable = {}

for key, value in pairs(tableOfValuesToFind) do --# iterate through all the keys and values
  lookupTable[value] = key --# store them into the lookup table
end
WhamyKaBlamy #3
Posted 05 January 2015 - 04:31 PM
Thank you very much MKlegoman :)/>/>

I thought that might be the case - Can I just check I'm understanding that right (as I'm very much used to using numbers for index keys)?

What that would be doing is setting the notes I'm using as the index key key for the bundled_output table?

So, where currently the index for my Bundled_Output would be:
Index Key = 1 –> Table value of "f#"
Index Key = 2 –> Table value of "1"
Index Key = 3 –> Table value of "g"
etc,

It would now be:
Index key = "f#" –> Table value of "1"
Index key = "g" –> Table value of "2"
Index key = "g#" –> Table value of "4"

Thank you again for the help :)/>/> I have to admit that if I'm correct with the understanding then if I'd taken the time to consider it I should have seen that was a solution *facepalm*

I may have been "zomg! No finding value function! *flail*"

Ammendment to previous post (New user, it hasn't been approved yet so I can't add it)

Thank you again MKLegoman - Just did what you suggested and it has cut the code down a huge amount and made it so much simpler:


local Look_Up_Direction = { ["f#"] = "L", ["g"] = "L", ["g#"] = "L", etc, etc}
local Look_Up_Value = { ["f#"] = 1, ["g"] = 2, ["g#"] = 4, etc, etc}
local Note_1 = {"f#","g","g#",etc, etc}
local Left_Bundled = 0
for i=1, #Note_1 do
	redstone.setBundledOutput("left",0)
	Left_Bundled = 0
	os.sleep(0.1)
	if Look_Up_Direction[Note_1[i]] == "L" then
		Left_Bundled = Left_Bundled + Look_Up_Value[Note_1[i]]
	end
	redstone.setBundledOutput("left",Left_Bundled)
	os.sleep(0.1)
end

Although with several more note lists and a second set of outputs for another group of noteblocks :)/>/>

I thought it might be useful to have my example up there for anyone that came across it.

And I'm currently listening to the first several bars of "I am the Doctor" from Doctor Who. *Is very happy*